我知道 shouldComponentUpdate()
在返回 false 时会阻止调用 render()
方法。
如果我在 render()
方法中使用一些昂贵的逻辑,那么 shouldComponentUpdate()
是一个非常有用的东西,因为我可以阻止 render()
打电话。
但是,如果我的 render()
方法只返回 Element
,而不执行任何昂贵的代码,那会怎样呢?
shouldComponentUpdate()
中的比较(让我们以 PureComponent 的实现为例)和 render()
调用时内置的 React diffing 之间有什么区别吗?
最佳答案
我觉得这里写的很棒: https://reactjs.org/docs/reconciliation.html
主要区别在于 React.PureComponent 在旧 props 和新 props 之间以及旧状态和新状态之间进行了浅层比较,但是内置的 React diffing(reconcilation)简要地比较了新旧 DOM 树,但请阅读上面这篇文章,它将解决您的疑惑。
关于javascript - shouldComponentUpdate 和内置 diffing 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257997/