Bind ; to a Class Method

I am pretty sure I have this code correct, but I am getting the following error:
Clicking the button element should run the addItem method and increment the state itemCount by 1.
// tests completed

Your code so far


class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      itemCount: 0
    };
    // change code below this line
    this.addItem = this.addItem.bind(this);

    // change code above this line
  }
  addItem() {
    this.setState({
      itemCount: this.state.itemCount + 1
    });
  }
  render() {
    return (
      <div>
        { /* change code below this line */ }
        <button onclick={this.addItem}>Click Me</button>
        { /* change code above this line */ }
        <h1>Current Item Count: {this.state.itemCount}</h1>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/bind-this-to-a-class-method

1 Like

Thanks! Sometimes a fresh pair of eyes is all you need!

1 Like

This was driving me crazy, thank you for the explanation. I was able to solve it but didn’t understand why and couldn’t figure out how to google an answer. Thank you!