组件被创建插入DOM中的时候,它的生命周期如下:
constructor()
static getDerivedStateFromProps()
render()
componentDidMount()
constructor()构造函数
组件被加载时,最先调用(仅仅一次)
其中的作用是定义状态变量
第一个语句为super(props)
componentWillMount()
组件即将被装载、渲染到页面上(render()被调用之前)调用(仅仅调用一次)
在函数中调用setState中改变状态,react等setState完成后再渲染组件
render()
在componentWillMount()之后,在componentDidMount()之前
生成虚拟的DOM节点
渲染挂载组件
初始化页面、接收新的props(父组件更新)
componentDidMount()
在render()之后被调用,仅仅调用一次
组件真正被装载之后。
componentWillUnmount