I was watching a tutorial on React and I came across this statement:
setTimeout(() => {
this.setState({name: "Bob"});
}, 1000)
Now, I'll admit I'm pretty new to JS in general so this might just be ignorance on the basics, but what is happening with () => {}
? I googled it without any luck. Outside references welcome.
This is not a React thing... arrow functions are new in es6 javascript. More info can be found here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Some basic info about arrow functions (taken from the above link):
An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.
Some basic syntax:
(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; }
// Parentheses are optional when there's only one parameter:
(singleParam) => { statements }
singleParam => { statements }
// A function with no parameters requires parentheses:
() => { statements }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With