React 生命周期
挂载时
挂载前 constructor
render
挂载后 componentDidMount
发起一个ajax 请求
定时器
更新时
render
DidUpdate
卸载时
componentWillUnmount
清定时 取消ajax请求 abort
axios
使用说明
挂载
1、constructor() 挂载前:初始化state 和为事件处理函数绑定实例
2、render()
3、componentDidMount() 挂载后:(插入 DOM 树中)立即调用,定时器,请求、对应
WillUnmount()
componentDidMount() 方法会在组件已经被渲染到 DOM 中后运行
更新
1、render() props 和state 变化 时
2、componentDidUpdate() 组件更新时调用
同样可以发起ajax 请求,但需要约束某种条件或符合某种条件
卸载
当组件从 DOM 中移除(组件卸载及销毁之前)时会调用
1、componentWillUnmount()清定时器、取消ajax请求,对应DidMount()
2. componentDidCatch(err, info) {} 获取子组件错误的函数