React 3 - setState()

  • React to achieve management by managing state assembly

 

  • Update state using this.setState () to achieve

 

  • this.setState () is called, React will again call the render method to render the UI.

If a parent component is called this.setState (), regardless of the sub-component has no parent component state objects in use, because the sub-assembly will re-render the parent component () and execute their render () method again. Functions components will be re-executed.

 

  • It is a state variable, state of the internal components to maintain a group of users to reflect changes in the set of UI component.

 

  • Props for assembly using it, it is read-only. Props modified value, only within parent components.

 

  • this.setState () is an asynchronous operation, and will not be executed immediately

 

For example figure, the same two output console

 

 

This problem can be bypassed by a callback function

 

 

 

 

 

  •  this.setState () formats
    • The first parameter is an object

 

    • The first parameter is a function

 

The second parameter is the effect of two way as above, can be in this.setState () callback function plus

 

  • to sum up
  • Each component can have its own state, state must first define to use. Modify the state must be achieved through this.setState ()

 

 

  • this.setState () must be the first argument or return a json. json the field we want to modify the state key, json's value is the new value of the state key

 

 

 

  • If state comprises a plurality json key, this.setState () can be modified only one or more corresponding key value.

 

 

 

  • Json typical operation

Appending to existing data

 

 

Delete the specified data array

 

 

 

 Update the object state. Note that, here, the addition of the brackets, using the variable name is the name of the target state

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/crdanding/p/12113132.html