This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
React 16 The React 16 life cycle is a process that can be used to implement the React 16 life cycle
preface
1. React
Three phases of the life cycle
Initialization phase > Update Phase > Uninstall phase
One, from the firstReact 16
The previous life cycle looks like
Here’s the old life cycle diagram:
Main life cycle interpretation:
componentWillMount
: A stage before the page is mountedcomponentDidMount
: The life cycle of a page after it has been mounted, usually through which data on the page can be initialized (such as network requests)componentWillRecieveProps
: This life cycle is often very earlyrender
Is not triggered, as the name implies, usually isThe parent component
The incomingprops
This lifecycle is triggered by a change that results in a re-rendering, but may result if the parent component is constantly passing stateprops
Keep refreshing, resulting in page endless loopshouldComponentUpdate
: Can be used for performance optimization by determining whether the state has changed to trigger rerenderingcomponentWillUpdate
: Update phase,shouldComponentUpdate
returntrue
After the triggercomponentDidUpdate
: The update is complete and the new element can be accessedcomponentWillUnmount
: Unloading stage, which can be used for some cleanup work
Second,16.3 the React
Life cycle of
Three,16.4 the React
Life cycle of
The following is16.4 the React
Life cycle diagram of:
The difference between React 16.3 and React 16.4 lifecycle. getDerivedStateFromProps runs through the Update phase in React 16.4, whereas in React 16.3 it is only affected by props
React 16.4 Lifecycle Comparison and improvements to older versions:
16.4 the React
Deprecated the following insecurities (UNSAFE
) life cycle:componentWillMount
: Usage error — incomponentWillMount
It is called immediately after the runrender
However, the data requested by the developer asynchronously is not rendered properly, causing problems with the first rendering of spatio-temporal datacomponentWillRecieveProps
: of the parent componentprops
This life cycle is triggered, but it is also triggered as soon as the parent component is re-rendered, and secondA child component that uses this life cycle falls into a rendering loop if calling a function passed by the parent causes the parent to re-rendercomponentWillUpdate
The first rendering of the component does not trigger this phase
16.4 the React
The following life cycles have been added:Static method static getDerivedStateFromProps
: Static methods are not inherited by component instances, so the method cannot passthis
To get the method or data of the component instance. This method is called before each rendering of the component, and the function returns an object to updatestate
The function is passed a new oneprops
And the currentstate
According to the newprops
Follow the developer logicstate
Make adjustments, and finally return the object pairstate
Update, returnnull
Does not update)getSnapshotBeforeUpdate
: Any return value from this method is passed as the third argumentcomponentDidUpdate
The life cycle is the equivalent ofDOM
Take a snapshot of the current state of the element before updating
Four, notes
componentWillRecieveProps
Is triggered not just because of the parent componentprops
Have changed. This method is triggered whenever the parent component re-renders, and it may cause the component to change the state of the parent component, causing an infinite loop of rendering- Don’t in
componentWillMount
Method because the mount process can beReact
Multiple interruptions mean multiple triggerscomponentWillMount
, causing memory leaks componentWillXXX
In the future it will be interrupted many times during the mount phase due to the asynchronous rendering mechanism, so methods that only trigger once (such as requests) should be placedcomponentDidMount
In theReact
The new version will ensure that users see the latest versionUI
Before the refreshcomponentWillUpdate
tocomponentDidUpdate
Between thesetState
The operation is carried out in this processsetState
This will result in a rendering loop