生命周期
类组件才有生命周期
创建时执行顺序
version 16.4
constructor -> render -> componentDidMount
触发时机 作用
constructor 最先执行 初始化数据
render 每次组件渲染 渲染UI(不建议调用setState,可能导致不断更新)
componentDidMount 组件挂载 发送请求 操作Dom
更新时执行顺序
三种情况可以让组件重新渲染
1. 新的props 2. setState方法 3.forceUpdate方法
render -> componentDidUpdate
触发时机 作用
render 组件更新时
componentDidUpdate 更新dom之后 获取dom 发送请求 (setState搭配if语句使用,注意一直更新)
组件卸载时执行顺序
componentWillUnmount 组件卸载前执行