概念:
组件从创建、到运行、再到销毁,这个过程中总是伴随着各种各样的事件,这些事件的统称就是组件的生命周期。
组件的生命周期大致分为三部分:
第一:组件的创建:仅执行一次;
第二:组件的运行:根据props或者state的变化,有选择性的执行一次或者多次;
第三:组件的销毁:仅执行一次。
组件生命周期图片:
生命周期函数介绍:
defaultProps
在组件创建之前,会先初始化默认的props属性,这是全局调用一次,严格地来说,这不是组件的生命周期的一部分。在组件被创建并加载候,首先调用 constructor 构造器中的 this.state = {},来初始化组件的状态。
React生命周期的回调函数总结成表格如下:
组件生命周期的执行顺序 及参数:
-
Mounting:
-
constructor()
-
componentWillMount()
-
render()
-
componentDidMount()
-
Updating:
-
componentWillReceiveProps(nextProps)
-
shouldComponentUpdate(nextProps, nextState)
-
componentWillUpdate(nextProps, nextState)
-
render()
-
componentDidUpdate(prevProps, prevState)
-
Unmounting:
-
componentWillUnmount()