Search code examples
javascriptreactjsarrow-functions

Are parentheses needed around the returned object in an arrow function in JavaScript?


In the React docs, I came across this example of an arrow function. After the =>, the state object is wrapped in parentheses. Are they necessary? And, if so, do the parentheses change how the arrow function works?

enter image description here

Different than?:

this.setState((prevState, props) => {
    //code
});

Would the code without the parentheses run properly?


Solution

  • The sample code returns an object.

    The longer version would be:

    this.setState((prevState, props) => {
      return { counter: ... };
    });