The problem of defining variables in react

if the variable defined in this.state is changed, it will be re-render
if you don"t want it to render just how to change the value, do you need to write it outside the component

?

change the this.state page and it will be re-rendered. If you don't want to re-render, you can set the this.state. Change the variable to this. Variable to do operations; or, as mentioned upstairs, forcibly prevent it from rendering through the shouldComponentUpdate lifecycle.


your needs can be achieved through shouldComponentUpdate () .
change state for render . Your need is very strange.


react has a shouldComponent Update in its life cycle. If you judge in this life cycle function that the value in this.state is different from the previous value, you can return false to prevent react from render again


state change will trigger re-render, you had better not save state as a demand variable.
shouldComponentUpdate () you can change render


in state this can only be limited within the cycle. State executes this.setState and render method is executed. If you don't want to, you can consider the condition mentioned above that render is not needed in the cycle. ShouldComponentUpdate must have a return value, otherwise an error will be reported that, return true executes render false and does not execute render. If you find it troublesome, you can consider the new method of naming components released in react version 16.
class Box extends React.PureComponent he can make some simple comparisons.


generally speaking, you can just hang it on this without this.state. But sometimes you have to hang some data that has nothing to do with rendering on state, such as getDerivedStateFromProps, so you can open a mutableArea object on state to hang variables. Directly modifying the properties of this object does not change itself, so it does not affect the rendering. It is more convenient than implementing shouldComponentUpdate.

Menu