7

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.

Max Power
  • 167
  • 1
  • 2
  • 12
  • 3
    [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Jonny Henly Jun 20 '16 at 18:59

1 Answers1

16

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 }
apokryfos
  • 38,771
  • 9
  • 70
  • 114
erichardson30
  • 4,984
  • 8
  • 26
  • 47