在做了这么多的React的需求之后,我意识到,自己对于react的了解真的只是浮于表面,没有深入理解内部的原理,今天又细细学习了关于生命周期的一些概念和原理:
React声明周期的四个大阶段:
Initialization
:初始化阶段。Mounting
: 挂在阶段。Updation
: 更新阶段。Unmounting
: 销毁阶段开发阶段主要用到的生命周期的一些函数:
挂载阶段:componentWillMount,componentDidMount
组件更新阶段:shouldComponentUpdate(这个函数还可以用来进行性能优化),componentWillUpdate,componentDidUpdate
销毁阶段:componentWillUnmount
另外还有一个componentWillReceiveProps 表示子组件接受父组件传递过来的参数,父组件render函数重新执行的时候触发
上面所提到的优化页面性能,减少render刷新的次数还可以利用shouldComponentUpdate函数
1 shouldComponentUpdate(nextProps,nextState){ 2 if(nextProps.content !== this.props.content){ 3 return true 4 }else{ 5 return false 6 } 7 }
原文:https://www.cnblogs.com/lianer88/p/11005058.html