React - click to change render method to display state's value

0 votes
186 views
added Mar 8, 2019 in React by LC Marshal Captain (25,790 points)

Now we’ll change the Square’s render method to display the current state’s value when clicked:

class Square extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: null,
    };
  }
  render() {
    return (
      <button className="square" 
      onClick={() => this.setState({value: 'x'}) }
      > 
        {this.state.value}
      </button>
    );
  }
}

export default Square;

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...