-
实例化阶段
constructor:
什么时候调用:在一开始实例化组件的时候调用
作用:初始化state
componentWillMount:
什么时候调用:即将加载组件的时候调用
作用:在render渲染组件的时候之前可以做事情
render:
什么时候调用:渲染组件的时候调用
componentDidMount:
什么时候调用:加载组件完成的时候调用
作用:在render之后做事情比如发送请求
注意:constructor,componentWillMount,componentDidMount
只会调用一次
-
运行阶段
componentWillReceiveProps:
什么时候调用:每次传入Props,就会调用
作用:拦截props
shouldComponentUpdate:
什么时候调用:每次props,或者state改变,就会调用
作用:控制是否刷新界面
componentWillUpdate:
什么时候调用:组件即将更新调用
作用:在render更新前做事情
componentDidUpdate:
什么时候调用:组件更新完成调用
作用:在render更新后做事情
注意:
以上运行阶段的方法都会来多次;
绝对不要在componentWillUpdate,componentDidUpdate中调用this.setState方法,否则将导致无限循环调用,但是可以在componentWillReceiveProps,shouldComponentUpdate。
-
销毁阶段
componentWillUnmount:
什么时候调用:组件即将销毁的时候调用
作用:移除观察者,清空数据,类似ios的dealloc
网友评论