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 {
    this.state = {count: 0};
    this.incrementCount = this.incrementCount.bind(this);
    this.setState({count: this.state.count + 1});
    return (
        <p>The current count is: {this.state.count}.</p>
        <button onClick = {()=>{this.incrementCount()}}>
          Add 1

export default ClassComponentState;

Leave a Reply

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