Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of `() => {}` in ReactJS [duplicate]

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.

like image 418
Max Power Avatar asked Jun 20 '16 18:06

Max Power


Video Answer


1 Answers

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 }
like image 103
erichardson30 Avatar answered Oct 20 '22 01:10

erichardson30