When returning an object from an arrow function, it seems that it is necessary to use an extra set of {}
and a return
keyword because of an ambigui
When you do are doing:
p => {foo: "bar"}
JavaScript interpreter thinks you are opening a multi-statement code block, and in that block, you have to explicitly mention a return statement.
If your arrow function expression has a single statement, then you can use the following syntax:
p => ({foo: "bar", attr2: "some value", "attr3": "syntax choices"})
But if you want to have multiple statements then you can use the following syntax:
p => {return {foo: "bar", attr2: "some value", "attr3": "syntax choices"}}
In above example, first set of curly braces opens a multi-statement code block, and the second set of curly braces is for dynamic objects. In multi-statement code block of arrow function, you have to explicitly use return statements
For more details, check Mozilla Docs for JS Arrow Function Expressions