【转】React Native 中组件的生命周期
React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps
执行过一次后,被创建的类会有缓存,映射的值会存在 getInitialState
控件加载之前执行,返回值会被用于state的初始化值 componentWillMount
执行一次,在初始化 render
render的时候会调用 componentDidMount
在初始化render之后只执行一次,在这个方法内,可以访问任何组件, 从这个函数开始,就可以和 JS 其他框架交互了,例如设置计时 setTimeout 或者 setInterval,或者发起网络请求 shouldComponentUpdate
boolean shouldComponentUpdate(
object nextProps,object nextState
)
这个方法在初始化
shouldComponentUpdate: function(nextProps,nextState) {
return nextProps.id !== this.props.id;
}
当 默认情况下, componentWillUpdatevoid componentWillUpdate(
object nextProps,object nextState
)
当 componentDidUpdatevoid componentDidUpdate(
object prevProps,object prevState
)
组件更新结束之后执行,在初始化 componentWillReceivePropsvoid componentWillReceiveProps(
object nextProps
)
当 componentWillReceiveProps: function(nextProps) {
this.setState({
likesIncreasing: nextProps.likeCount > this.props.likeCount
});
}
componentWillUnmount
当组件要被从界面上移除的时候,就会调用 总结React Native的生命周期就介绍完了,其中最上面的虚线框和右下角的虚线框的方法一定会执行,左下角的方法根据 英文地址:https://facebook.github.io/react/docs/component-specs.html#lifecycle-methods 原文链接地址: http://blog.csdn.net/ElinaVampire/article/details/51813677 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |