react生命周期(v15 - v16)对比

阶段v15v16.8
挂载阶段constructorconstructor
componentWillMountgetDerivedStateFromProps
renderrender
componentDidMountcomponentDidMount
更新阶段componentWillReceivePropsgetDerivedStateFromProps
shouldComponentUpdateshouldComponentUpdate
componentWillUpdaterender
rendergetSnapshotBeforeUpdate
componentDidUpdatecomponentDidUpdate
卸载阶段componentWillUnmountcomponentWillUnmount
异常处理getDerivedStateFromError
componentDidCatch

v15 --> v16
废弃三个生命周期(未删除,UNSAVE_前缀):componentWillMountcomponentWillUpdatecomponentWillReceiveProps

新增了三个生命周期:getDerivedStateFromPropsgetSnapshotBeforeUpdatecomponentDidCatch

getDerivedStateFromProps
这是个静态方法,此方法必须要有返回值,返回一个对象,或者返回 null,返回 null 则不更新任何内容。
当我们接收到新的属性想去修改state, 可以使用 getDerivedStateFromProps

getSnapshotBeforeUpdate(prevProps,prevState)
这个在render方法之后,Update之前调用,有两个参数prevProps,prevState,表示之前的props和之前的state,这个函数有一个返回值,会作为第三个参数传给componentDidUpdate,如果你不想要返回值,可以返回null,此生命周期必须与componentDidUpdate搭配使用(新增生命周期)。

static getDerivedStateFromError: 此声明周期会在渲染阶段后代组件抛出错误后被调用,他将抛出的错误作为参数,并返回一个值以更新 state
componentDidCatch: 此生命周期在后代组件抛出错误后背调用,他接受两个参数: error —— 抛出的错误。2. info —— 带有 componentStack key 的对象,其中包含有关组件引发错误的栈信息。componentDidCatch 会在“提交”阶段被调用,因此允许执行副作用。 它应该用于记录错误之类的情况。

图示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值