javascript – React parenthesis syntax (({count}) => ({count: count + 1}))

Question:

I don't understand this syntax, I met in setState :

this.setState (({ count }) => ({ count: count + 1 }))

And also in the functional component:

const Link = ({ count }) => ({
    render: () => (
        ...
    )
})

Those. the same count argument, I understand why in curly braces, we get this.state and do destructuring, but why the body of the arrow function is wrapped in braces and curly braces?

At first glance, it seems to return an object, but why is the method of this object written through an arrow function?

Answer:

The body of the arrow function is additionally wrapped in parentheses because otherwise the curly braces would not be considered a literal of the object to be returned, but simply a constraint on the function body.

For instance:

 var first = () => { counter: 10 }; function firstFun() { // эквивалент функции выше counter: 10 } var second = () => ({ counter: 10 }); function secondFun() { // эквивалент функции выше return { counter: 10 }; } console.log(first()); console.log(firstFun()); console.log(second()); console.log(secondFun());
Scroll to Top