8

These functions seem to get used interchangeably in React tutorials but can't figure out the difference... or when to use which?

const SomeVal = () => {
    ...
}

const SomeVal = () => (
    ...
)
chris358
  • 139
  • 5
  • the first one you replace ... with code that your function should run. – Reda Meskali Aug 09 '18 at 18:18
  • 7
    I assume you're referring to arrow functions (`() => {}` or `() => ()`). The former creates a block so the arrow function can have multiple statements. The latter is an implicit return of an expression wrapped in the grouping operator. – Andrew Li Aug 09 '18 at 18:20
  • This is a question I to would like to know the answer to. – SudoKid Aug 09 '18 at 18:21
  • 1
    @Li357 you should make that an answer as it's the answer. – SudoKid Aug 09 '18 at 18:22

2 Answers2

8

These are fundamentally different. The former arrow function syntax, () => {}, allows you to have multiple statements inside the arrow function's body, i.e.:

() => {
  console.log('Hello!');
  return 'World!';
}
// When called, this logs Hello! and returns World!

But the latter, () => (), is an arrow function that implicitly returns an expression that is wrapped in parentheses (called the grouping operator). It does not explicitly allow for multiple statements:

() => (
  'World' // only one expression that is implicitly returned
  // Would work fine without parentheses (unless you want to return an object!)
)
// When called, this function returns 'World'

Of course, you could also create an unreadable arrow function that performs multiple operations using the comma operator:

() => (console.log('Hello!'), 'World!')

React

I assume you see this with React stateless components:

const Comp = () => (
  <div>Hello World!</div>
);

This function (components are just functions) returns the <div> element implicitly. But, using {}, you can do some intermediary calculations instead of just returning right away:

const Comp = () => {
  const msg = 'Hello World!';
  return ( // Need to explicitly return here
    <div>{msg}</div>
  );
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Not that much less readable IMO with formatting, especially if it's only a few statements. I agree probably not practical in production code, but useful for golfing/fun – Anthony Aug 09 '18 at 18:32
2

An arrow function with const fn = () => { ... } creates a code block where you can have multiple lines of code. However for this case you need to have a return statement.

An arrow function with const fn = () => ( ... ); supports a single line of code and the return statement is implicit. The brackets around the function are optional so your function could look like

const sum = (a, b) => a + b;
const sum2 = (a, b) => (a + b);

const sumBlock = (a, b) => { return a + b; }

In essence, both do the same thing. But, the latter way of writing arrow functions allows your code to be slightly more concise if you only need a single statement.

Adam
  • 1,724
  • 13
  • 16
  • Note that you can also have multiple lines when using `()`, comma separated with the last item being what is returned (no `return` statement). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Anthony Aug 09 '18 at 18:29