-2
class MyButton extends Component {

    state = { tapped: false }
    tap() {
          this.setState({ tapped: true });  // --> 'this' here is undefined 
    }
    render() {
        return (
             <button onClick={ this.tap }>TAP ME</button>
        )
    }
}

I know with ES6 class React doesn't auto bind this to component's instance, hence inside tap "this" is undefined. What I don't understand here is why tap function is present in the component's instance and is not throwing an error undefined of tap doesn't exist? How tap is part of component instance?

Akash Verma
  • 264
  • 3
  • 11

1 Answers1

-2
class MyButton extends Component {

    state = { tapped: false }

    tap = () => {
          this.setState({ tapped: true }); 
    }

    render() {
        return (
             <button onClick={ this.tap }>TAP ME</button>
        )
    }
}
Daniel Khoroshko
  • 2,213
  • 12
  • 21