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)