Why parentheses here but not here

Why no parentheses here:

toggleVisibility() {
    this.setState(state => {
      if (state.visibility === true) {
         return { visibility: false };
       } else {
         return { visibility: true };

But here there are:

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



Accoding to MDN

It looks like you “Parenthesize the body of a function to return an object literal expression”.

1 Like

the first one, you have explicit return, so the graph parenthesis are of the function body

the second one you have implicit return and the {} are of an object, if you don’t put the round parenthesis around the object, the graph parenthesis are considered those of the function body, and as such name: "value" is wrong sintax loose inside a function

Why does this code not work:

reset() {
      this.setState(state => ({count: 0}))

And this code does:

reset() {
      count: 0

I fixed it like this:

reset() {
      this.setState(state => ({count: state.count - state.count}))

But is this the only way?

here you do not have a function…

state.count - state.count makes 0, you can just have count: 0 inside that object instead of count: state.count - state.count