React生命周期

旧生命周期

原理图:
在这里插入图片描述
可见,react包含如下生命周期函数:
1.constructor()
2.componentWillMount()
3.render()
4.componentDIdMount()
5.componentWillUnmount()
6.componentWillReceiveProps()
7.shouldComponentUpdate()
8.componentWillUpdate()
9.componentDIdUpdate()
其中3,4,5是最常用的,带“”will“”的2,6,8官方不推荐使用,并且提示加前缀UNSAFE_使用。

重要的钩子

render()初始化渲染或更新渲染调用
componentDIdMount()开启监听,发送Ajax请求,开启定时器等
componentWillUnmount()做一些收尾工作,清理定时器等

即将废弃的钩子

componentWillMount()
componentWillReceiveProps()
componentWillUpdate()

新生命周期

原理图:
在这里插入图片描述
果然是移除了那三个钩子,新增了两个 :
1.getDerivedStateFromProps()
2.getSnapshotBeforeUpdate()
1.getDerivedStateFromProps()使用场景:若state的值在任何时候都取决于props,那么可以使用
2.getSnapshotBeforeUpdate():在更新之前获取快照。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值