react生命周期函数

生命周期函数定义:在某一时刻组件会自动调用执行的函数

四个过程:

1.初始化过程就是constructor构造函数,生命周期中只执行一次。

2.Mounting挂载过程:componentWillMount->render->componentDidMount, 组件初始化后就会执行一次(生命周期中只会执行一次)挂载过程

3.updation更新过程:会执行多次

这个过程分两块来看,对于顶级组件,没有父组件,调用setState更新状态时,先会执行shouldComponentUpdate, 该函数返回true,则继续执行componentWillUpdate->render->componentDidUpdate.

对于子组件来说,props是父组件给子组件传递的属性,每当父组件调用了render方法渲染一次,子组件的componentWillReceiveProps函数就会执行一次,但是第一次父组件render的时候例外,很好理解,因为这是Updation过程,而第一次渲染出来并不是更新过程。同样componentWillReceiveProps执行结束,接着执行shouldComponentUpdate->componentWillUpdate->render->componentDidUpdate

4.卸载过程,组件卸载之前执行一次componentWillUnmount,执行后,组件就会在dom中被移除

 

 

总结:一个组件开始第一次生命周期,展示出来,是经过了初始化过程----》挂载过程,之后调用setState改变状态,或者其父组件render一次,该组件本身就要进行一次Updation更新过程。

在父组件中用条件渲染隐藏组件,就会进入卸载unmounting过程,将组件卸载

posted @ 2019-12-16 20:15  盛俊勇  阅读(362)  评论(0)    收藏  举报