Why we don't need to bind the arrow function in React?

前端 未结 2 1411
遇见更好的自我
遇见更好的自我 2020-12-10 06:41

We all know that we need to bind function in React to make it work. I do know why do we need to bind it.

But I\'m not sure why we don\'t need to bind arrow function

2条回答
  •  独厮守ぢ
    2020-12-10 07:19

    Simply because arrow function does not have the following in its context:

    • this
    • arguments
    • super
    • new.target

    So when you reference this inside an arrow function it treat this as any other variable and look for its declaration in its scope first and it can not find it so it search the upper scope which is the this referring to the react component class which what is required so we do not need to bind the this to the class.

提交回复
热议问题