0

It's common knowledge that when defining a React component, you need to bind your class methods inside the class constructor in order to be able to access this.props, etc. inside those methods.

My question is simply "why?" In JS, isn't this defined to be the method's object when calling the function as a method on an object?

Ersin Akinci
  • 164
  • 1
  • 6
  • 1
    Depends on how the function is called. – Kevin B Jul 03 '18 at 21:42
  • *"In JS, isn't `this` defined to be the method's object when calling the function as a method on an object?"* That's right. But event handlers are usually not called as methods. You are just passing a function as event handler and the event handling systems calls that function like a normal function (it cannot do anything else). So it's on you to ensure that `this` is correct in your event handler. If the method is not used as event handler then it's likely not necessary to `.bind` it. – Felix Kling Jul 03 '18 at 21:51
  • Thanks, @FelixKling! That made sense. – Ersin Akinci Jul 03 '18 at 22:22

0 Answers0