2

I tried googling this and searching on stack overflow, but none of these really seemed to help with my solution. I am trying to set up two buttons. When each one is clicked I want it to render its respective component. Here is what I am currently doing to change the state.

import React, { Component } from 'react';
import Structured from '../Structured/Structured';
import YC from '../YC/YC';

class Home extends Component {
constructor() {
  super();
  this.state = {
    YC: false,
    Structured: false
  }
}

ycClick() {
    this.setState({
      YC: true,
      Structured: false
    });
  }


structuredClick() {
    this.setState({
      Structured: true,
      YC: false
    });
  }


render() {
  const { isSignedIn, route } = this.state;
  return (
    <div>
    <h1>Rick's Notes</h1>
    <div class="ph3">
        <a class="f6 link dim br-pill ph3 pv2 mb2 dib white bg-black pa2 ma2" href="#0" onClick={this.ycClick}>YC vs. Non. YC</a>
        {this.state.YC ? <YC /> : null}
        <a class="f6 link dim br-pill ph3 pv2 mb2 dib white bg-black pa2 ma2" href="#0" onClick={this.structuredClick}>Structured Note Descriptions</a>
        {this.state.Structured ? <Structured /> : null}
    </div>
    </div>
  );
}
}

export default Home;

I am trying to make it so that on click YC (or structured) is set to true and if this.state.yc is true it returns the component. However, it says that this is undefined so their must be an issue with the way I am calling the component. Thanks for any help. Let me know if there's anything else I should specify.

3
  • What exactly is it saying is undefined? Can you post the error message? Commented Jun 14, 2018 at 17:51
  • can you show structured/YC component code? Commented Jun 14, 2018 at 17:51
  • You have to bind ycClick() to the correct this. For example in ctor: this.ycClick = this.ycClick.bind(this); Commented Jun 14, 2018 at 17:52

2 Answers 2

7

Use this.ycClick.bind(this) instead of this.ycClick (same with this.structuredClick)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I tried running this and I got "TypeError: this.state is not a function" on the ycClick function.
1

Update your handler with this

ycClick = () => {
    this.setState({
      YC: true,
      Structured: false
    });
  }


structuredClick = () => {
    this.setState({
      Structured: true,
      YC: false
    });
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.