react生命周期(自我总结)

一、React17.0之前

  1. componentWillMount()
  • 执行场景:render()之前
  • 说明:
    (1)componentWillMount在render之前执行,所以在该方法中setState不会发生重新渲染;
    (2)该方法是服务端渲染中执行的唯一方法;
    (3)一般用constructor()代替。
  1. render()
  • 执行场景:
    (1)componentWillMountz()之后;
    (2)componentWillReceive(nextProps, nextState)之后。
  1. componentDidMount()
  • 执行场景:render()方法之后
  • 说明:
    (1)在render之后立即执行;
    (2)在该方法之中对DOM进行操作,执行之后ref变成实际的DOM;
    (3)在该方法之中请求数据,若使用了redux之类的数据服务,在此处可以发出action;
    (4)此处setState()可以触发重新渲染。
  1. componentWillReceiveProps(nextProps)
  • 执行场景:
    (1)在已经挂载的组件(mounted component)接收到新的props时触发;
    (2)该方法在初始化render时不会被调用。
  • 说明:
    (1)大部分情况下,该生命周期是没有用的,可以略去不写;
    (2)若是在constructor函数中初始化了某个state,必须用componentWillReceiveProps来更新state;
    (3)调用componenWillReceiveProps的时候,不要去向上分发,调用父组件的相关setState方法,否则会造成死循环。
  1. shouldComponentUpdate(nextProps, nextState)
  • 执行场景:在接收到到新的props或者state时,或者说在componentWillReceiveProp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值