ComputerScientistC
Reactiflux4y ago
4 replies
ComputerScientist

✅ – Alexs – 10-05 Dec 15

I'm currently reading the React docs ( the state States and Lifecycle section) and I just want to have some clarity on a specific part of this code.

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}


On the handleClick() function is the setState lifecycle just taking the "this.state" property of your class and switching it to the negated value?
Was this page helpful?