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

0 votes
314 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:

  1. class Square extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. value: null,
  6. };
  7. }
  8. render() {
  9. return (
  10. <button className="square"
  11. onClick={() => this.setState({value: 'x'}) }
  12. >
  13. {this.state.value}
  14. </button>
  15. );
  16. }
  17. }
  18.  
  19. export default Square;

 

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