挂载卸载过程相关生命周期
- constructor()
通常情况在组件实例化时,首先执行该生命周函数用来初始化组件的state, props 等信息。同时由于ES6与ES5 在继承机制上有多不同:
ES5的继承, 实质实现构建子类的实例对象this, 然后在将父类的可继承属性及方法添加到这个this上面(Parent.apply(this))。
ES6的继承, 实质是先创建父类的实例对象this(必须先调用super方法), 然后再用子类的构造函数修改this。
如果子类没有定义constructor 方法, 这个方法会被默认添加(也就是说不管有没有显示定义,任何一个子类都有constructor方法)。
class ColorPoint extends Point {
}
// 等同于
class ColorPoint extends Point {
constructor(...args) {
super(...args);
}
}
// 可见没有写constructor,在执行过程中会自动补上
总结:
(1) 如果组件要定义自己的state初始状态的话, 需要写在constructor钩子函数中;
(2) 可以不写constructor, 一旦写了constructor, 就必须在此函数中写super(),
此时组件才会有自己的this, 否则只写constructor而不执行 super() 那么以后的this 都是错的。
(3) 在custructor 生命周期中如果不需要使用 this.props 或者 props , 可以不传如props。
- componentWillMount()
该生命周期使用比较少, 更多的是在服务端渲染时使用。 它表示的是组件已经历constructor() 初始化数据后, 但还未渲染DOM时。
此时Dom 还没有挂载。 但该周期内可以访问到 window, document 等api
- componentDidMount()
组件第一次渲染完成,此时dom节点已经生成,可以进行相关的DOM操作,可以在这里调用ajax请求,返回数据setState后组件会重新渲染
- componentWillUnmount()
在此处完成组件的卸载和数据的销毁。
(1)clear你在组建中所有的setTimeout,setInterval
(2)移除所有组建中的监听 removeEventListener
(3)有时候我们会碰到这个warning:
更新过程
- componentWillReceiveProps(nextProps)
(1)在接受父组件改变后的props需要重新渲染组件时用到的比较多
(2)接受一个参数nextProps
(3)通过对比nextProps和this.props,将nextProps的state为当前组件的state,从而重新渲染组件。
- shouldComponentUpdate(nextProps, nextState)
(1)主要用于性能优化(部分更新)。
(2)唯一用于控制组件重新渲染的生命周期,由于在react中,setState以后,state发生变化,组件会进入重新渲染的流程,在这里return false可以阻止组件的更新。
(3)因为react父组件的重新渲染会导致其所有子组件的重新渲染,这个时候其实我们是不需要所有子组件都跟着重新渲染的,因此需要在子组件的该生命周期中做判断。
- componentWillUpdate(nextProps, nextState)
shouldComponentUpdate返回true以后,组件进入重新渲染的流程,进入componentWillUpdate,这里同样可以拿到nextProps和nextState。
- componentDidUpdate(prevProps, prevState)
组件更新完毕后,react只会在第一次初始化成功会进入componentDidmount,之后每次重新渲染后都会进入这个生命周期,这里可以拿到prevProps和prevState,即更新前的props和state。
- render()
render函数会插入jsx生成的dom结构,react会生成一份虚拟dom树,在每一次组件更新时,在此react会通过其diff算法比较更新前后的新旧DOM树,比较以后,找到最小的有差异的DOM节点,并重新渲染。
React 新增的生命周期
- getDerivedStateFromProps(nextProps, prevState)
代替componentWillReceiveProps()。
老版本中的componentWillReceiveProps()方法判断前后两个 props 是否相同,如果不同再将新的 props 更新到相应的 state 上去。这样做一来会破坏 state 数据的单一数据源,导致组件状态变得不可预测,另一方面也会增加组件的重绘次数。
// before
componentWillReceiveProps(nextProps) {
if (nextProps.isLogin !== this.props.isLogin) {
this.setState({
isLogin: nextProps.isLogin,
});
}
if (nextProps.isLogin) {
this.handleClose();
}
}
// after
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.isLogin !== prevState.isLogin) {
return {
isLogin: nextProps.isLogin,
};
}
return null;
}
componentDidUpdate(prevProps, prevState) {
if (!prevState.isLogin && this.props.isLogin) {
this.handleClose();
}
}
这两者最大的不同就是:
在 componentWillReceiveProps 中,我们一般会做以下两件事,一是根据 props 来更新 state,二是触发一些回调,如动画或页面跳转等。
在老版本的 React 中,这两件事我们都需要在 componentWillReceiveProps 中去做。
而在新版本中,官方将更新 state 与触发回调重新分配到了 getDerivedStateFromProps 与 componentDidUpdate 中,使得组件整体的更新逻辑更为清晰。而且在 getDerivedStateFromProps 中还禁止了组件去访问 this.props,强制让开发者去比较 nextProps 与 prevState 中的值,以确保当开发者用到 getDerivedStateFromProps 这个生命周期函数时,就是在根据当前的 props 来更新组件的 state,而不是去做其他一些让组件自身状态变得更加不可预测的事情。
- getSnapshotBeforeUpdate(prevProps, prevState)
代替componentWillUpdate。
常见的 componentWillUpdate 的用例是在组件更新前,读取当前某个 DOM 元素的状态,并在 componentDidUpdate 中进行相应的处理。
它的含义是在React更新Dom元素之前,获取一个快照,它返回的结果将作为componentDidUpdate的第三个参数。一般的用法就是获取更新前的DOM。
这两者的区别在于:
在 React 开启异步渲染模式后,在 render 阶段读取到的 DOM 元素状态并不总是和 commit 阶段相同,这就导致在
componentDidUpdate 中使用 componentWillUpdate 中读取到的 DOM 元素状态是不安全的,因为这时的值很有可能已经失效了。
getSnapshotBeforeUpdate 会在最终的 render 之前被调用,也就是说在 getSnapshotBeforeUpdate 中读取到的 DOM 元素状态是可以保证与 componentDidUpdate 中一致的。
此生命周期返回的任何值都将作为参数传递给componentDidUpdate()。
React 生命周期流程图
- 原始生命周期图
- 新生命周期图