【react】生命周期(旧)

生命周期的三个阶段:

  1. 初始化阶段:由ReactDOM.render()触发——初次渲染
  • constructor() 构造器
  • componentWillMount() 组件将要挂载
  • render() 初始化渲染和状态更新之后调用,调1+n次
  • componentDidMount() 常用 组件挂载完毕之后调用,只调一次 render的兄弟
    一般在这个钩子中做一些初始化的事:开启定时器、发送网络请求、订阅消息
  1. 更新阶段:由组件内部this.setState()或父组件重新render触发
  • shouldComponentUpdate() 控制组件更新的"阀门" 返回false时,页面就不会更新了,后面的生命周期也不会再执行
  • componentWillUpdate() 组件将要更新的钩子
  • render() 必须用的一个
  • componentDidUpdate() 组件更新完毕的钩子
  1. 卸载组件:由ReactDOM.unmountComponentAtNode()触发
  • componentWillUnmount() 常用
    一般在这个钩子中做一些收尾的事:关闭定时器、取消订阅消息

(以上是官方版本)
以下是我自己总结的,更详细点:

进入页面后生命周期的执行顺序
constructor 构造器

componengtWillMount 组件将要挂载

render 初始化渲染和状态更新之后调用,调1+n次

componentDidMount 组件挂载完毕之后调用,只调一次 render的兄弟

componentWillUnmount 组件将要卸载 可以用来清除定时器

以下是走setState之后的生命周期执行顺序
执行setState

shouldComponentUpdate 控制组件更新的"阀门" 返回false时,页面就不会更新了,后面的生命周期也不会再执行

componentWillUpdate 组件将要更新的钩子

render

componentDidUpdate 组件更新完毕的钩子

componentWillUnmount 组件将要卸载

以下是forceUpdate —— 不更改任何状态中的数据,强制更新的生命周期的执行顺序
执行forceUpdate

componentWillUpdate 组件将要更新的钩子

render

componentDidUpdate 组件更新完毕的钩子

父组件给子组件传值时生命周期的执行顺序
父组件的render

子组件的componentWillReceiveProps 但是第一次传值不会走这个生命周期,第二次传才会走

子组件的shouldComponentUpdata

子组件的componentWillUpdate

子组件的render

子组件的componentDidUpdate

其他生命周期
ReactDom.unmountComponentAtNode 卸载组件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值