react on use of setState

React official reference document, where do the next record

(1) setState grammar

setState (updater, [callback]) 

Analysis:
  parameter is a parameter having the form of  updater a function:
  Parameter Two optional callback function

  1, a parameter description

  

  Note: Of course, it should not be modified directly. You should use based  state and  props new objects built to represent change. For example, suppose we want According  props.step to increase state:

  

 

  updater function received  state and  props are guaranteed up to date. updater return value to  state be shallow merger .

  

 

  

  So we generally use will use its simple syntax

Simple syntax is as follows:
the this
.setState ({ counter })

  2, problem: when to use a parameter object format, when using the format function?

  The following cases, the cumulative effect of two forms of realization

  

Object shorthand way is a function of the way 
if the new state is not dependent on the original state ===> using an object mode 
if the new state is dependent on the original state ===> use functions mode 
If you need to get the latest state (after) in setState data, in the second read callback function

  About dependent and independent, as follows:

  

 

  

 

  3, introduced two parameters

setState () of the second parameter is optional callback function, it will complete merger setState and re-rendering performed after assembly. 
In general, we recommend the use of componentDidUpdate () instead of this method.
Parameters II: callback function performs timing: after the callback interface status updates and update.

 

  

  

 

 

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/12629894.html