Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need an extra set of parentheses for React .setState()?

I'm going through the "Quick Start" portion of React's website, and it uses code that looks something like this:

toggleClicked() {
    this.setState(prev => ({
        isOn: !prev.isOn
    }));
}

Why do I need the extra parentheses around the function block? I would think that it would look like this:

toggleClicked() {
    this.setState(prev => {
        isOn: !prev.isOn
    });
}

But this doesn't work. What do these parentheses mean?

like image 530
JakAttk123 Avatar asked Mar 23 '18 03:03

JakAttk123


People also ask

Why do you need to use setState () to update state in React?

Always use the setState() method to change the state object, since it will ensure that the component knows it's been updated and calls the render() method.

What happens when you make multiple setState () calls within a function?

Because of all the work actually involved when the setState() function is called, React may batch multiple setState() calls into a single update to improve performance. Thanks to this batching process, this. state might be updated asynchronously which means you cannot always rely on the current this.

What does setState () do and how does it work?

Syntax: We can use setState() to change the state of the component directly as well as through an arrow function. Example 1: Updating single attribute. We set up our initial state value inside constructor function and create another function updateState() for updating the state.

What is the second argument of setState in React?

The second argument that can optionally be passed to setState is a callback function which gets called immediately after the setState is completed and the components get re-rendered.


2 Answers

The interpreter considers the { after => to be the start of a function block, rather than an object - so, wrap it in parentheses to make it clear that you're returning an object there, rather than defining a function.

this.setState(prev => ({
    isOn: !prev.isOn
}));
like image 87
CertainPerformance Avatar answered Oct 26 '22 23:10

CertainPerformance


The braces around isOn: !prev.isOn is the object literal notation.

The parentheses immediately surrounding that is mandatory to indicate that the above braces represent an object literal instead of a function body. This distinction is necessary because the right hand side of a "fat arrow" (=>) can either be a proper function body (surrounded by braces) or an expression (which is an implicit return). In the latter case, it's possible for that expression to be an object literal, which will be confusing for the parser (I'm sure the TC 39 committee studied the language grammar and concluded that it's not possible for the parser to distinguish the two use cases without the help of explicit parentheses).

The next outer parentheses are the delimiters of the arguments passed to setState.

The outer most pair of braces are the delimiters of the function body of toggleClicked.

like image 26
Huy Nguyen Avatar answered Oct 27 '22 01:10

Huy Nguyen