React 生命周期
第一个阶段:初始创建阶段
-
constructor() 接收父级的props,初始化state 只执行1次
-
componentWillMount 组件渲染前 17.0 版本即将被废除了 只执行1次
-
render 渲染 执行多次 dom创建或更新,触发render进行diff算法,比较最小化差异,进行更新
-
componentDidMount 渲染完毕 只执行一次请求数据 获取到真实的dom
第二个阶段:更新阶段
-
props更新
-
componentWillReceiveProps(nextProps) props修改之后会触发
-
shouldComponentUpdate(nextProps,nextState) 组件是否要更新?必须要返回一个boolean ,返回值为true时,组件会更新,返回false,不更新
-
componentWillUpdate 组件更新前 17.0 版本即将被废除了
-
render 渲染
-
componentDidUpdate 组件更新完毕
第三个阶段:销毁阶段
- componentWillUnmount 组件即将销毁
清除定时器,清除添加事件
组件优化
-
1.尽量不要在render内声明函数
-
2.在shouldComponentUpdate,如果props或state没有修改,不更新组件