016.React生命周期

旧生命周期

1.生命周期图

react生命周期(旧).png

2.渲染流程

(1).初始化阶段

由ReactDOM.render()触发----初次渲染react

constructor() 调用构造器
constructor()中完成了React数据的初始化,它接受两个参数:props和context,当想在函数内部使用这两个参数时,需使用super()传入这两个参数。(实际开发通常省略构造器)
注意:只要使用了constructor()就必须写super(),不然会致使this指向错误。ajax

componentWillMount()
组件已经经历了constructor()初始化数据后,可是还未渲染DOM。(通常较少使用)算法

render()
DOM元素渲染(通常不把render函数放在生命周期中,这里仅表示在这个时期会执行render)安全

componentDidMount()
组件第一次渲染完成,此时dom节点已经生成,能够在这里调用ajax请求,返回数据setState后组件会从新渲染,通常作一些初始化的事,如开启定时器 发起网络请求 订阅消息等。性能优化

(2).更新阶段

由组件内部this.setState()或父组件从新render触发markdown

componentWillReceiveProps(nextProps)
在接受父组件改变后的props须要从新渲染组件时会用到
接受一个参数nextProps,经过对比nextProps和this.props,将nextProps的state为当前组件的state,从而从新渲染组件网络

shouldComponentUpdate(nextProps,nextState)dom

  • 这里通常主要用于性能优化(部分更新)
  • 这个生命周期能够用于控制组件从新渲染,当setState之后,state发生变化,组件会进入从新渲染的流程,在这里return false能够阻止组件的更新
  • 由于react父组件的从新渲染会致使其全部子组件的从新渲染,这个时候其实咱们是不须要全部子组件都跟着从新渲染的,所以须要在子组件的该生命周期中作判断

componentWillUpdate(nextProps,nextState)
shouldComponentUpdate返回true之后,组件进入从新渲染的流程,进入componentWillUpdate,这里一样能够拿到nextProps和nextState。异步

render()
render函数会插入jsx生成的dom结构,react会生成一份虚拟dom树,在每一次组件更新时,在此react会经过其diff算法比较更新先后的新旧DOM树,比较之后,找到最小的有差别的DOM节点,并从新渲染。函数

componentDidUpdate(prevProps,prevState)
组件更新完毕后,react只会在第一次初始化成功会进入componentDidmount,以后每次从新渲染后都会进入这个生命周期,这里能够拿到prevProps和prevState,即更新前的props和state。

(3).组件卸载阶段

由ReactDOM.unMountComponentAtNode()触发

componentWillUnmount()
通常作一些收尾的事 如关闭定时器 取消订阅消息
clear你在组建中全部的setTimeout,setInterval
移除全部组建中的监听 removeEventListener

新生命周期

1.生命周期图

react生命周期(新).png

2.渲染流程

(1).初始化阶段:

由ReactDOM.render()触发 ---初次渲染

constructor()

getDerivedStateFromProps()
代替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,二是触发一些回调,如动画或页面跳转等。

  1. 在老版本的 React 中,这两件事咱们都须要在 componentWillReceiveProps 中去作。
  2. 而在新版本中,官方将更新 state 与触发回调从新分配到了 getDerivedStateFromProps 与 componentDidUpdate 中,使得组件总体的更新逻辑更为清晰。并且在 getDerivedStateFromProps 中还禁止了组件去访问 this.props,强制让开发者去比较 nextProps 与 prevState 中的值,以确保当开发者用到 getDerivedStateFromProps 这个生命周期函数时,就是在根据当前的 props 来更新组件的 state,而不是去作其余一些让组件自身状态变得更加不可预测的事情。

使用:

static getDerivedStateFromProps(props,state){
    // 从props获得一个派生的状态
    // 使用场景:若state的值在任什么时候候都取决于props 则可使用
    console.log(state) // giveCar:'toyota'
    console.log(props) // giveCar:'加长林肯'
    console.log('A--getDerivedStateFromProps')
    return (null)
    // 返回props时 状态state会更改成props 且不会再改变
}
复制代码

render()

componentDidMount()

(2).更新阶段:

由组建内部this.setState()或父组件从新render触发

getDerivedStateFromProps()

shouldComponentUpdata()

render()

getSnapshotBeforeUpdata(prevProps,prevState)
代替componentWillUpdate。
常见的 componentWillUpdate 的用例是在组件更新前,读取当前某个 DOM 元素的状态,并在 componentDidUpdate 中进行相应的处理。
这二者的区别在于:

  1. 在 React 开启异步渲染模式后,在 render 阶段读取到的 DOM 元素状态并不老是和 commit 阶段相同,这就致使在componentDidUpdate 中使用 componentWillUpdate 中读取到的 DOM 元素状态是不安全的,由于这时的值颇有可能已经失效了。
  2. getSnapshotBeforeUpdate 会在最终的 render 以前被调用,也就是说在 getSnapshotBeforeUpdate 中读取到的 DOM 元素状态是能够保证与 componentDidUpdate 中一致的。
    今生命周期返回的任何值都将做为参数传递给componentDidUpdate()。

使用:

// 在更新以前获取快照 获取更新以前想要的状态
getSnapshotBeforeUpdate(){
    console.log('A--getSnapshotBeforeUpdate')
    return '快照snapshot' //传递给生命周期的下游 componentDidUpdate
}
复制代码

componentDidUpdata()

(3)卸载组件:

由ReactDOM.unMountComponentAtNode()触发
componentWillUnmount() 经常使用 通常作一些收尾的事 如关闭定时器 取消订阅消息

相关文章
相关标签/搜索