生命周期
Mount 已经插入真实DOM
Updating 正在被重新渲染
Unmounting 已移出真实DOM
生命周期方法
初始化时调用
componentWillMount 在初始化render时调用
componentDidMount 在初始化render后调用
更新时调用
componentWillReceiveProps 在组件接收到一个新的props时调用
shouldComponentUpdate 返回一布尔值用于决定是否执行render
componentWillUpdate 在render前调用(非初始化)
componentDidUpdate 在render后调用(非初始化)
移除时调用
componentWillUnmount 组件从DOM中移除时调用
应用举例
通过在shouldComponentUpdate方法进行对部分数据项进行对比,如若相同则返回false本次不执行后续的render,若不同才进行更新,这一方法可防止重复的渲染,节约资源。
参考资料:React 组件生命周期 | 菜鸟教程 (runoob.com)