0

I am learning react and in some videos the lecturers use function like this in their component.

function(){} 

and sometimes they use arrow function like this

function=()=>{}

what are the different between these two? whenever I use function(){} I cannot call any props from state so I always use arrow function and it works very well.

1

2 Answers 2

2

Arrow function automatically binds this-context to your component. With the normal function you would need to do that yourself in your components constructor like that:

this.func = this.func.bind(this)
Sign up to request clarification or add additional context in comments.

Comments

1

When using the "fat arrow" function, i.e. myFunc = () => {}, if you try to access this you will receive access to the surrounding function's this. This is because the fat arrow function does not bind it's own this.

When you use myFunc() {} you are binding this and scoping it to that function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.