The official documentation

  1. SetState (updater[, callback]) setState(updater[, callback])

  2. The first argument is an updater function with The signature:(state, props) => stateChange

const updater = (prevState, prevProps)=>{
     return {counter: prevState.counter + prevProps.step};
}
Copy the code

That is:

const updater = (prevState,prevProps) => ({counter: Prevprevprevprops. Step}) // note: this is (), not {}, ES6 dictate format, in order to differentiate code block.Copy the code

That is:

this.setState(
 (prevState, prevProps)=>{
     return {counter: prevState.counter + prevProps.step};
   }
)
this.setState( 
(prevState, prevProps)=>
( {counter: prevState.counter + prevProps.step};)
)
Copy the code

This is not equivalent to removing the prevState argument, but why React? Because React is its own definition. He handled it in setState, not in a natural transition. See the screenshot of the official website below.

this.setState(
         {counter: prevState.counter + prevProps.step};
)
Copy the code

The difference is that if you use the updater function, updating render is immediate. The folks at Facebook say that if state is based on the previous prevState, to ensure updates, recommend instead!