首页 > 解决方案 > shouldComponentUpdate 是否会阻止 getDerivedStateFromProps?

问题描述

我正在更新一个遗留组件,它使用:

  1. shouldComponentUpdate()避免昂贵的状态重新计算
  2. componentWillUpdate()如果1通过,则进行重新计算并渲染

文档说_

ifshouldComponentUpdate()返回false, then UNSAFE_componentWillUpdate(), render(), andcomponentDidUpdate()不会被调用`

getDerivedStateFromProps()并未提及。我想用它来替换已弃用的componentWillUpdate().

我的问题是:shouldComponentUpdate()阻止调用的方式与它的调用getDerivedStateFromProps()方式相同componentWillUpdate()吗?

注意:我知道将记忆化视为另一种解决方案。

标签: reactjs

解决方案


当传递的 props changegetDerivedStateFromProps()函数被调用时,然后shouldComponentUpdate().

但是,如果状态发生变化,只会shouldComponentUpdate()触发生命周期函数。

您可以在此处查看组件生命周期图以获得更多帮助


推荐阅读