Why setState doesn´t work like the other functions?

I´m practicing the react challenges and it says it should be declared like this:

this.setState({
      name: "React Rocks!"
    })

I´m shocked why is not like this:

this.setState(){
      name: "React Rocks!"
    }

Or better because the previous one wouldn´t make 100% sense since “name” wouldn´t have global scope:

this.setState(){
      this.state.name= "React Rocks!"
    }

Why it has curly braces INSIDE parethensis? In my life I haven´t seen anything like that. Is not because it have to wrap it because of JSX since setState is declared outside return, so that doesn´t explain why,right?

setState() is a function, and functions can accept parameters.

You are simply providing setState() with an object as a parameter.

Maybe the new lines and indentation threw you off, but theese are all the same.

this.setState({
      name: "React Rocks!"
})
this.setState({ name: "React Rocks!" })
let obj = {
    name: "React Rocks!"
} 
this.setState(obj)
1 Like

setState is a function that accepts a argument type of object.

yeah, they definetely confused me lol! How couldn´t i see that. All understand it now :grin:

1 Like