Reputation: 21
This line works as the content after arrow in wrapped in ()
this.setState((prevState) => ({toggle:prevState.toggle + 1}))
The content after arrow is not wrapped in () so it is not working
this.setState((prevState) => {toggle:prevState.toggle + 1})
Upvotes: 1
Views: 94
Reputation: 12222
Arrow functions may implicitly return values by simply omitting the curly braces that traditionally wrap a function's body if their body only contains a single expression.
const foo = x => x + 1;
foo(1); // -> 2
When using implicit returns, object literals must be wrapped in parenthesis so that the curly braces are not mistaken for the opening of the function's body.
const foo = () => { bar: 1 } // foo() returns undefined
const foo = () => ({ bar: 1 }) // foo() returns {bar: 1}
Upvotes: 4
Reputation: 1292
When you want to implicitly return an object, curly braces do not work because they denote start and end of block. In order to distinguish between block and JS object, you wrap the object in ().
Upvotes: 1