Bind 'this' to a Class Method

Tell us what’s happening:

I am not sure why this is returning undefined inside the additem. this should have been accessible inside the component.

Your code so far


class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      itemCount: 0
    };
    // change code below this line
console.log("here")
this.addItem = this.addItem.bind( this )
    // change code above this line
  }
  addItem() {
    console.log(`this is ${this}`) . *//this is undefined*
    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 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:

sorry but I did not understand