React入门教程 - 组件生命周期
http://fraserxu.me/2014/08/31/react-component-lifecycle/ 每一个React组件在加载时都有特定的生命周期,在此期间不同的方法会被执行。 组件加载: componentWillMountcomponentWillMount()
由于这个方法始终只执行一次,所以如果在这里定义了 组件加载: componentDidMountcomponentDidMount()
这个方法会在组件加载完毕之后立即执行。在这个时候之后组件已经生成了对应的DOM结构,可以通过 如果你想和其他JavaScript框架一起使用,可以在这个方法中执行 componentDidMount: function() {
setTimeout(function() {
this.setState({items: {name: 'test'}})
},1000)
}
组件更新: componentWillReceivePropscomponentWillReceiveProps(object nextProps)
在组件接收到一个新的prop时被执行。这个方法在初始化render时不会被调用。 Use this as an opportunity to react to a prop transition before render() is called by updating the state using this.setState(). (不太懂这句话。。。) 旧的props可以通过 componentWillReceiveProps: function(nextProps) {
this.setState({
likesIncreasing: nextProps.likeCount > this.props.likeCount
});
}
组件更新: shouldComponentUpdateboolean shouldComponentUpdate(object nextProps,object nextState)
返回一个布尔值。在组件接收到新的props或者state时被执行。在初始化时或者使用 可以在你确认不需要更新组件时使用。 shouldComponentUpdate: function(nextProps,nextState) {
return nextProps.id !== this.props.id;
}
如果 默认情况下 By default,shouldComponentUpdate always returns true to prevent subtle bugs when state is mutated in place,but if you are careful to always treat state as immutable and to read only from props and state in render() then you can override shouldComponentUpdate with an implementation that compares the old props and state to their replacements.(这句也不太懂。。。) 如果你需要考虑性能,特别是在有上百个组件时,可以使用 组件更新: componentWillUpdatecomponentWillUpdate(object nextProps,sans-serif; font-size:18px; line-height:27px"> 在组件接收到新的props或者state但还没有render时被执行。在初始化时不会被执行。
一般用在组件发生更新之前。 组件更新: componentDidUpdatecomponentDidUpdate(object prevProps,object prevState)
在组件完成更新后立即执行。在初始化时不会被执行。一般会在组件完成更新后被使用。例如清除notification文字等操作。 Unmounting: componentWillUnmount在组件从DOM unmount后立即执行. componentDidMount:function(){
this.inc = setInterval(this.update,500)
},componentWillUnmount:function(){
console.log("goodbye cruel world!")
clearInterval(this.inc)
}
主要用来执行一些必要的清理任务。例如清除 ref:http://facebook.github.io/react/docs/component-specs.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |