美文网首页
RN学习-组件的生命周期以及作用

RN学习-组件的生命周期以及作用

作者: 马戏团小丑 | 来源:发表于2017-10-20 17:17 被阅读57次
  • 实例化阶段

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

相关文章

网友评论

      本文标题:RN学习-组件的生命周期以及作用

      本文链接:https://www.haomeiwen.com/subject/bwvvuxtx.html