This article is a review of the React Life cycle. It contains notes from the React Engineer’s Guide on the Life cycle.
1. Three stages of the life cycle:
The React component has three phases in its lifecycle:
- Mounting Phase (Mounting) : This phase starts with the component initialization and continues until the component is created and rendered into the real Dom.
- Updating: This phase starts with component updates and continues until component updates are complete and re-rendered to the real Dom.
- Unmounting: In this stage, the listener component is unmounted from the Dom.
2. Life cycle functions hanging in phases:
constructor(props)
: inherited fromComponent
Class, don’t forget to addsuper
static getDerivedStateFromProps
(fromprops
To derivestate
🤣) : can be obtained during the mount phaseThe current props and state
And then according toprops
Come to the rightstate
Pay attention to the following problems when using:- Internal unusable
this
; - This method is in
The React of 16.3
Only after version - There must be a return value, and the return value is correct
state
Is equivalent to a call elsewheresetStatus()
When passed in to modify the object. - The component must be available initially
state
attribute
- Internal unusable
componentWillMount
The component will be mounted- React16.3 is not recommended for use after React16.3
UNSAFE_ComponentWillMount
- Can’t and
getDerivedStateFromProps
At the same time use render
: Generates a virtual DOM from the contents of the return and submits it toReactDOM
Render the real DOM.
- React16.3 is not recommended for use after React16.3
componentDidMount
: Component has been mounted, virtualdom
Has been added to the realdom
In the now.
import React, { Component } from 'react';
class App extends Component {
constructor(props){
super(props);
console.log("1- Component initialization");
this.state = {
nba:"Kobe"
};
}
// 🚀 cannot be used with the willMount method
static getDerivedStateFromProps(props,state){
console.log("2- map props to state");
return {nba:iverson}; }componentWillMount(){
console.log("3- Components about to be mounted");
}
render(){
console.log("4- Generate virtual DOM based on return");
return <h1>hello react</h1>
}
componentDidMount(){
console.log("5- Component mounted, virtual DOM added to real DOM"); }}export default App;
Copy the code
3. Life cycle function of update stage:
There are three types of updates: parent component updates, component updates, and forceUpdate
3.1 Parent Component Update:
🚀 16.3 before:
Parent component updates cause child component updates in the order:
ComponentWillReceiveProps – > shouldcomponentUpdate – > componentWillUpdate – > render – > comoponentDidUpdate
🚀 16.3:
getDerivedStateFromProps
Method instead ofcomponentWillReceiveProps
;componentWillReceiveProps
andcomponentWillUpdate
It was gradually abandoned
After 🚀 16.3:
GetDerivedStateFromProps –> shouldcomponentUpdate –> Render –> getSnapshoutBerforUpdate –> componentDidupdate
getSnapshoutBerforUpdate(prevProps,prevState)
: it is inrender
Method to generateVirtual dom
And then, renderReal dom
Before, right nowthis.props
andthis.state
It’s updatedprops
andstate
.- In addition, this method must have a return value, which is passed to
componentDidUpdate
import React, { Component } from 'react';
class Child extends Component {
state = {
name: "child"
}
getSnapshotBeforeUpdate(prevPorps,prevState){
console.log("4- DOM for getting updates");
return {
info: "Information to pass to componentDidUpdate."}}{info:" Information to pass to componentDidUpdate "}
componentDidUpdate(prevPorps,prevState,snapshot){
console.log("5- Component finisher",snapshot);
}
render(){
/ /... slightly}}export default Child;
Copy the code
3.2 Component Self-update:
Self-update of a component is primarily an update of the current component caused by a call to the setStatus method. There have also been 3 versions of changes:
🚀 16.3 before:
ShouldComponentUpdate –> compoonentWillUpdate –> render –> componentDidUpdate Listen state modification)
🚀 16.3:
shouldComponentUpdate
—> render
—> getSnapshotBeforeUpdate
—> componentDidUpdate
🚀 16.4 and beyond:
static getDerivedStateFromProps
—> shouldComponentUpdate
—> render
—> getSnapshotBeforeUpdate
—> componentDidUpdate
3.3 Forced Update:
If the dependent data is not state or props and the data changes and the view is expected to change, use forceUpdate.