React - Set State with this.setState

Tell us what’s happening:

What is ‘this’ referencing to in the bind method?
this.handleClick = this.handleClick.bind(this);

Your code so far

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'Initial State'
    };
    this.handleClick = this.handleClick.bind(this); //<--- Here
  }
  handleClick() {
    // Change code below this line

    // Change code above this line
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click Me</button>
        <h1>{this.state.name}</h1>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

React - Set State with this.setState

frodo_keepYourSecrets_meme

If I had to guess, I would say this in the bind method would refer to the class itself.

Hi @jobanibusiness

this is an article you may find helpful.

Happy coding

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.