In our programming process, the most important thing for React learning is the life cycle, and learning the react life cycle will also be of great help to our own programming.
If you have a good foundation, it is recommended to look directly at the code https://github.com/facebook/react/blob/master/src/renderers/dom/tests /ReactDOMProduction-test.js#L89-L175 , as can be seen from the code The generation process of ReactDOM, the life cycle of React is described in detail below:
- instantiate
getDefaultProps
calls React.createClass, then triggers the getDefaultProps method, which returns an object, which is then merged with the props object specified by the parent component, and finally assigned to this.props as the default property of the component, this method is called only oncegetInitialState
initializes the value of state, the return value will be assigned to this.state, in this method, you can already access this.props.componentWillMount
operates on state and will not trigger re-rendering. It is recommended to use constructor insteadRender
generates the virtual DOM structure required by the page according to the value of statecomponentDidMount
can set state, which will trigger re-rendering. Inside the component, ReactDOM.findDOMNode(this) can be used to get the node of the current component to operate the DOM
- Existence
componentWillReceiveProps(nextProps)
This function is triggered when the component receives new props. Usually, this.setState method can be called to compare the execution state of this.props and nextProps, and complete the modification of the stateshouldComponentUpdate(nextProps, nextState
This method is used to intercept new props or state, and then determine whether to update the componentcomponentWillUpdate(nextProps, nextState)
is called before the updaterende
generates virtual DOM data that needs to be updated according to the diff algorithmcomponentDidUpdate(prevProps, prevState)
After the render method is successfully executed, the real DOM will be rendered. You can use the this.getDOMNode() method in this method to access the native DOM
- Destruction & Cleanup Period
- componentWillUnmount
will trigger componentWillUnmount, usually to remove the DOM, cancel event binding, destroy timers, etc.
References:
Understanding React ComponentsReact
Component LifecycleReact
Lifecycle Resolution
The article is from my github: https://github.com/junhey/studyNotes/issues/24