Write a Simple Counter -- Increment/Decrement not working

Your code here

class Counter extends React.Component 
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
    // change code below this line
    this.increment = this.increment.bind(this);
    this.decrement = this.decrement.bind(this);
    this.reset = this.reset.bind(this);
    // change code above this line
  }
  // change code below this line
  increment() { this.setState ({
    count: this.setState + 1})
  }
  decrement() { this.setState ({
    count: this.setState - 1})
  }
  reset() { this.setState ({
    count: 0})
  }
  // change code above this line
  render() {
    return (
      <div>
        <button className='inc' onClick={this.increment}>Increment!</button>
        <button className='dec' onClick={this.decrement}>Decrement!</button>
        <button className='reset' onClick={this.reset}>Reset</button>
        <h1>Current Count: {this.state.count}</h1>
      </div>
    );
  }
};

Test results
For Increment, following error is displayed –
Current Count: function(a,b){"object"!==typeof a&&"function"!==typeof a&&null!=a?p("85"):void 0;this.updater.enqueueSetState(this,a,b,`"setState")}1"

For Decrement, it returns NaN

Reset works as expected

Here’s your bug.

I’d advise reading the officials docs about how to change the state in React applications.

2 Likes

Thank you @Gilbert1391 . Got it working.