reactjs - shouldComponentUpdate 是否会阻止 getDerivedStateFromProps?
问题描述
我正在更新一个遗留组件,它使用:
shouldComponentUpdate()
避免昂贵的状态重新计算componentWillUpdate()
如果1通过,则进行重新计算并渲染
文档说_
if
shouldComponentUpdate()
返回false
, thenUNSAFE_componentWillUpdate()
,render()
, andcomponentDidUpdate()
不会被调用`
但getDerivedStateFromProps()
并未提及。我想用它来替换已弃用的componentWillUpdate()
.
我的问题是:shouldComponentUpdate()
阻止调用的方式与它的调用getDerivedStateFromProps()
方式相同componentWillUpdate()
吗?
注意:我知道将记忆化视为另一种解决方案。
解决方案
当传递的 props changegetDerivedStateFromProps()
函数被调用时,然后shouldComponentUpdate()
.
但是,如果状态发生变化,只会shouldComponentUpdate()
触发生命周期函数。
您可以在此处查看组件生命周期图以获得更多帮助
推荐阅读
- swift - Swift 不透明类型作为返回类型?
- python - 如何使用请求更改某些内容的 HTML 值?
- docker - 无法在 Windows 上提取硒图像
- javascript - 无法通过 DiscordJS 中的不同范围传递变量
- pdfbox - PFType0Font 和 PFType1Font 文件大小的区别
- javascript - 如何修复数据表数据正在更新或销毁表?
- python - Docker compose 无法在 mac 上构建特定服务
- flutter - 颤动中MaterialButton上的条件highlightColor
- c++ - 在 Windows 上使用带有 SendInput 函数的字符串
- javascript - Knockout JS:在表格中点击编辑