组件的生命周期
组件创建阶段 (只执行一次)
componentWillMount() 还未渲染DOM
componentDidMount() 组件第一次渲染完成,dom节点已生成
组件运行阶段 (按需,根据props属性或state状态的改变,有选择性的执行0或多次)
componentWillReceiveProps (nextProps) 父组件修改属性时触发,可以修改新属性和状态
shouldComponentUpdate(nextProps,nextState) 返回false会阻止render调用
componentWillUpdate (nextProps,nextState) 不能修改属性和状态
componentDidUpdate(prevProps,prevState) 可以修改dom
组件销毁阶段 (只执行一次)
componentWillUnmount() 组件的卸载和数据销毁
(1)clear所有定时器
(2)移除组件中的监听事件
(3)有时候我们会碰到这个warning:
Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
原因:因为你在组件中的ajax请求返回setState,而你组件销毁的时候,请求还未完成,因此会报warning
解决方法:
componentDidMount() {
this.isMount === true
axios.post().then((res) => {
this.isMount && this.setState({ // 增加条件ismount为true时
aaa:res
})
})
}
componentWillUnmount() {
this.isMount === false
}
4907

被折叠的 条评论
为什么被折叠?



