前言:React 的版本从 v15 到 v16.3 ,再到v16.4,如今最新的版本是 v16.8了。其中最大的变化多是React Hooks
的加入,而最使人困惑的倒是它的生命周期,新旧生命周期函数混杂在一块儿,不免会让许多新来者有不少困惑。因此这一篇咱们来分析一下 React 生命周期的演变及缘由,进一步理解其使用。react
这些方法会在组件初始化的时候被调用,只跟实例的建立有关。
若是用createReactClass
进行建立,则还有getInitialState
这些生命周期函数,但不多使用,咱们这里不说起。算法
定义默认props
,会和父组件传下来的props
进行合并,且以父组件中的props
优先级更高,至关于{...defaultProps, props}
。segmentfault
定义props
的数据类型,能够帮助咱们肯定其有效性,减小许多开发中没必要要的错误。浏览器
在加载阶段前调用一次,进行 state 的初始化。性能优化
constructor(props){ super(props) }
super(props)
用来调用父类的构建方法。网络
新版中为UNSAFE_componentWillMount()
。dom
只在组件加载时调用,整个生命周期只调用一次,之后组件更新也不会调用,此时能够修改 state。async
react
中最重要的生命周期函数,建立虚拟 dom,并进行 diff
算法,更新 dom 树也在此进行。因此不该该在此改变组件的状态或者与浏览器进行交互。ide
注意:这个函数不能缺乏,若是不建立虚拟 dom,能够return null
。函数
组件加载完成后当即调用,整个生命周期只调用一次,能够获取到更新后的 DOM,在此处能够进行网络请求等。
新版中为UNSAFE_componentWillReceiveProps()
。
在组件加载后,若是有新的props
传递过来,会先调用这个函数,能够在这里调用setState()
修改state
。
componentWillReceiveProps(nextProps)
react
中一个重要的性能优化点,组件接收到新的props
或者state
,返回true
表示须要更新 dom,返回false
阻止更新。
shouldComponentUpdate(nextProps, nextState)
新版中为UNSAFE_componentWillUpdate()
。
组件加载时不调用,只有在组件须要更新(即shouldComponentUpdate
返回true
)时调用。
componentWillUpdate(nextProps, nextState)
注意:不能在这个方法中调用setState()
修改state
。
同上。
在组件更新完成后当即被调用,能够进行网络请求等。
componentDidUpdate(prevProps, prevState)
在组件被卸载和销毁以前调用,能够在这里处理任何须要的清理工做,好比解除定时器,取消已经发起的网络请求,清理在componentDidMount
函数中建立的 DOM 元素。
componentWillUnmount()
错误边界捕获,在v16.0刚推出的时候新增长的一个生命周期函数,用于捕获在子组件树中任意地方发生的 JavaScript 错误,一个错误边界不能捕获它本身内部的错误。
componentDidCatch(error, info)
import React, {Component} from 'React' export default class OldReactComponent extends Componet{ static defaultProps={} static propTypes={} constructor(props){ super(props) } state={} componentWillMount(){} render(){ return null } componentDidMount(){} componentWillReceivePorps(nextProps){} componentShouldUpdate(nextProps,nextState){ return true } componentWillUpdate(nextProps,nextState){} componentDidUpdate(){} componentWillUnmount(){} }
(带UNSAFE_
的函数在之后将会被丢弃,官方也不建议使用,这里再也不列出。)
新版本中,新增了三个生命周期函数:
同时deprecate了一组生命周期API,包括:
能够看出除了shouldComponentUpdate
以外,render
以前的全部生命周期都被消灭了。缘由是这些生命周期太多时候没有被正确使用,并且在Fiber
以后,若是要开启async rendering
,在render
函数以前的全部函数,都有可能被执行屡次。
同上。
由于是静态方法,因此没法访问到组件实例。每次组件调用render
前都会被调用,获取新的props
和state
(v16.3只会为props
的而调用,v16.4修正为props
和state
)以后,返回一个对象做为新的state
,若是返回null
则表示不须要更新;配合componentDidUpdate,能够覆盖componentWillReceiveProps的全部用法。而且它应该是个纯函数,没有反作用(side effect)。
static getDerivedStateFromProps(props,state)
同上。
同上。
同上。
同上。
同上。
触发时间: update发生的时候,在render以后,在组件dom渲染以前;返回一个值,做为componentDidUpdate的第三个参数;配合componentDidUpdate, 能够覆盖componentWillUpdate的全部用法,经常使用于 scroll 位置的定位。
getSnapshotBeforeUpdate(prevProps, prevState)
同上。
同上。
同上。
用于在“render”阶段(非render
函数)的错误捕获,应该是个纯函数,没有反作用(side effect)。
static getDerivedStateFromError(error)
import React, {Component} from 'React' export default class OldReactComponent extends Componet{ static defaultProps={} static propTypes={} constructor(props){ super(props) } state={} static getDerivedStateFromProps(props,state){} render(){ return null } componentDidMount(){} componentShouldUpdate(nextProps,nextState){ return true } getSnapshotBeforeUpdate(prevProps, prevState){} componentDidUpdate(){} componentWillUnmount(){} }
static getDerivedStateFromProps
、getSnapshotBeforeUpdate
并弃用componentWillMount
,componentWillReceiveProps
,componetWillUpdate
(这三个函数将在 React 17中删除)。static getDerivedStateFromError
捕获“render”阶段的错误。帮你理清React的生命周期
清晰大图请点击这里