shouldComponentUpdate调用两次

shouldComponentUpdate调用两次

在这里插入图片描述

为什么生命周期钩子有的会调用多次

react 高版本(比如18)会在开发模式中加入严格模式

注意:
这仅适用于开发模式。生产模式下生命周期不会被调用两次。

严格模式下,这意味着 React 可以在提交之前多次调用渲染阶段生命周期的方法,或者在不提交的情况下调用它们(由于出现错误或更高优先级的任务使其中断)。

渲染阶段的生命周期包括以下 class 组件方法:

  • constructor
  • componentWillMount (or UNSAFE_componentWillMount)
  • componentWillReceiveProps (or UNSAFE_componentWillReceiveProps)
  • componentWillUpdate (or UNSAFE_componentWillUpdate)
  • getDerivedStateFromProps
  • shouldComponentUpdate
  • render
  • setState 更新函数(第一个参数)

所以严格模式检查仅在开发模式下运行;它们不会影响生产构建。
(build后,是正常调用一次)

参考官方:严格模式(https://zh-hans.reactjs.org/docs/strict-mode.html#gatsby-focus-wrapper)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值