javascript - 覆盖 PureComponent 的 shouldComponentUpdate 的问题
问题描述
例如我想写这样的东西:
class MyComponent extends PureComponent {
shouldComponentUpdate(nextProps, nextState, nextContext) {
console.log('shouldComponentUpdate');
return super.shouldComponentUpdate(nextProps, nextState, nextContext);
}
}
但是 super.shouldComponentUpdate 是未定义的。
- 为什么 super.shouldComponentUpdate 未定义?
- 如何调用 PureComponent 的默认实现?
反应 16.0.0
解决方案
进一步扩展维克多的回应。React 中 PureComponent 和 Component 的区别在于,纯组件通过对 props 和 state 进行浅层比较来处理 shouldComponentUpdate。
如果您想提供自定义 shouldComponentUpdate 来处理何时以及何时不应该更新组件,我建议您只使用常规组件。我还要注意 PureComponent 和 Component 都需要一个构造函数,该构造函数将 super() 调用作为第一行。
推荐阅读
- html - 如何在 html 中为 AngularJS 中的多个 json 值添加公共标题
- python - a.transpose().ravel()[0]=x 不能改变numpy中原始数组的值?
- excel - 在 Numbers/Excel 中,如何仅对匹配特定条件的行执行 SUMPRODUCT
- javascript - 如何对所有分页项目应用过滤器并仅显示过滤后的项目?
- apache-spark - pyspark creating BlockMatrix from matrices of different size
- r - 没有显示闪亮的情节
- javascript - 如何在没有 jquery 的情况下制作滑块图片库
- javascript - 为什么 e.clientX 和 e.clientY 函数在 mouseOver 上不能很流畅地输出?
- amazon-web-services - How to write SNS topic into CloudWatch log within AWS console?
- java - 尝试运行基本的 hello world 应用程序时出错