美文网首页
react 生命周期

react 生命周期

作者: 木中木 | 来源:发表于2019-11-09 20:49 被阅读0次

react主要思想通过构建可复用组件来构建界面,所谓组件就是有限状态机(FSM),有限个状态以及这些状态之间的转移和动作等行为的模型。react正是利用这个概念来实习通过管理状态对组件进行管理。废话不多说,下面看这些生命周期在不同的组件阶段,他们的执行顺序是如何

  • 初始化组件
    getDefaultProps -> getInitialState -> componenWillMount -> render -> componentDidMount
  • 第二次渲染
    getInitialState -> componenWillMount -> render -> componentDidMount
  • props changed
    ComponentWillRecieveProps -> shouldComponentUpdate ->render->componentDidupdate
  • state changed
    shouldComponentUpdate ->render->componentDidupdate
  • 卸载
    ComponentunMount

自定义组件生命周期主要通过三个阶段来完成,Mounting、recieveProps、unMounting。

  • Mounting
    这个阶段负责管理生命周期中的getInitialState / componentWillMount / render/ componentDidMount,由于getDefaultProps是在构造方法里面调用,所以getDefaultProps只在初始化组件的时候调用,以后都不在调用,这里要注意不要在componentWillMount调用setState方法,因为此时setState是不会触发re-render,而是会对state进行合并操作,而且此时state也不是最新的,因为 instance.state = this._processPendingState是在componentWillMount之后执行,只有render的时候才是最新
  • RecieveProps
    这个阶段负责管理生命周期中的 componentWillRecieveProps / componentShouldUpdate/ componentWillUpdate / render / componentDidUpdate, 这里要注意在componentWillRecieveProps / componentShouldUpdate/ componentWillUpdate中都不能调用setState,除了效果跟上述componentWillMount一样之外,在componentShouldUpdate、componentWillUpdate和render中调用setState还会触发死循环,因为这两生命周期中调用setstate,此时this._pendingStateQueue!=null,会触发调用updateComponent,而updateComponent又会调用componentwillUpdate和componentShouldUpdate
  • unMount
    此阶段会执行并重置所有的参数、更新队列和状态,清除公共类,完成组件卸载工作。此阶段调用setState不会re-render

react16最新生命周期

新的生命周期
Mounting(加载阶段:涉及4个钩子函数)
constructor()

加载的时候调用一次,可以初始化state
static getDerivedStateFromProps(props, state)

组件每次被rerender的时候,包括在组件构建之后(虚拟dom之后,实际dom挂载之前),每次获取新的props或state之后;每次接收新的props之后都会返回一个对象作为新的state,返回null则说明不需要更新state;配合componentDidUpdate,可以覆盖componentWillReceiveProps的所有用法
render()

react最重要的步骤,创建虚拟dom,进行diff算法,更新dom树都在此进行
componentDidMount()
组件渲染之后调用,只调用一次
Updating(更新阶段:涉及5个钩子函数)

static getDerivedStateFromProps(props, state)
组件每次被rerender的时候,包括在组件构建之后(虚拟dom之后,实际dom挂载之前),每次获取新的props或state之后;每次接收新的props之后都会返回一个对象作为新的state,返回null则说明不需要更新state;配合componentDidUpdate,可以覆盖componentWillReceiveProps的所有用法
shouldComponentUpdate(nextProps, nextState)

组件接收到新的props或者state时调用,return true就会更新dom(使用diff算法更新),return false能阻止更新(不调用render)

render()
react最重要的步骤,创建虚拟dom,进行diff算法,更新dom树都在此进行

getSnapshotBeforeUpdate(prevProps, prevState)
触发时间: update发生的时候,在render之后,在组件dom渲染之前;返回一个值,作为componentDidUpdate的第三个参数;配合componentDidUpdate, 可以覆盖componentWillUpdate的所有用法

componentDidUpdate()
组件加载时不调用,组件更新完成后调用

Unmounting(卸载阶段:涉及1个钩子函数)
组件渲染之后调用,只调用一次
Error Handling(错误处理)

componentDidCatch(error,info)
任何一处的javascript报错会触发

相关文章

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • 《深入React技术栈》学习笔记Ⅲ

    以下的生命周期都是在 React 15 的生命周期, React 16 的生命周期 API 已经发生变化。Reac...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

网友评论

      本文标题:react 生命周期

      本文链接:https://www.haomeiwen.com/subject/bcgnbctx.html