Errata: Listing 4-12


In listings 4-12 and 4-13, the button’s onClick calls this.incrementCount and passed count+1 to it. There’s no need to pass a value here, since the incrementCount function increments the count variable.

The corrected / improved code for listing 4-12 should be:

import {Component} from 'react';

class ClassComponentState extends Component {
  constructor(props){
    super(props);
    this.state = {count: 0};
    this.incrementCount = this.incrementCount.bind(this);
  }
  incrementCount(){
    this.setState({count: this.state.count + 1});
  }
  render(){
    return (
      <div>
        <p>The current count is: {this.state.count}.</p>
        <button onClick = {()=>{this.incrementCount()}}>
          Add 1
        </button>
      </div>
    );
  }
}

export default ClassComponentState;

Leave a Reply

Your email address will not be published. Required fields are marked *