2

Why is ESLint rejecting this?

let myFunc = (state) => {a:"b"};

It is saying that it expected a function or assignment call.

Oliver Watkins
  • 11,228
  • 22
  • 97
  • 192
  • Possible duplicate of [ECMAScript6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript6-arrow-function-that-returns-an-object) – JJJ Dec 11 '18 at 12:24

2 Answers2

3

In ES6 arrow functions

If you use curly braces {}, you should return with a return statement.

let myFunc = (state) => {
  return { a:"b"}
}

If you use don't use braces, you should enclose the return object with round braces ().

let myFunc = (state) => ({ a:"b"})
Dinesh Pandiyan
  • 5,293
  • 2
  • 27
  • 44
1

An arrow function returning an object literal in this way is syntactically ambiguous, as it could also be a JavaScript block with the label a in it. You need to surround the literal with parens to make it clear:

let myFunc = (state) => ({a:"b"});
cody
  • 10,743
  • 3
  • 19
  • 35