Set State with this.setState

Set State with this.setState
0.0 0

#1

Tell us what’s happening:

Your code so far


class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'Initial State'
    };
    this.handleClick = this.handleClick.bind(this.setState());
  }
  handleClick() {
    // change code below this line
  this.setState({
    name:'React Rocks!'
  });
    // 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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/set-state-with-this-setstate


#2

Hi @murtaza63

You want to bind handleClick to this not this.setState(). Have a look here to see what bind actually does.