Skip to content Skip to sidebar Skip to footer

This Keyword In Reactjs And Why We Loose The Context

I've red about this keyword, but i can't understand why in this case when i click on the button this is undefined? In this case this should point to the class Foo , why it is wind

Solution 1:

handleClick = (event)=>{
    console.log(this); //
  }

This is due to context use arrow function

handleClick is call in the context of click event of button, due to this refers to window context in handleClick, but when you use arrow function it will solve the issue

Post a Comment for "This Keyword In Reactjs And Why We Loose The Context"