ruby-on-rails - 如何在 ES5 中实现 PureComponent
问题描述
一点上下文:Rails Application running react-rails on sprockets (No es6) React 版本 15.4.1。
这意味着我们的组件定义如下:var ComponentName = React.createClass(...
我正在尝试使用回调将状态从子组件传递到父组件。问题开始是因为父状态的更新导致子组件重新渲染导致堆栈级别太深错误。
有人告诉我,我可以让子组件停止使用 PureComponent 重新渲染,但我还没有在 ES5 中找到实现。
有人对此有任何了解吗?
解决方案
PureComponent 基本上只是一种自动shouldComponentUpdate
对 props 和 state 进行浅层比较的方法。虽然您不能将 PureComponent 与 React.createClass 一起使用,但您可以使用 shouldComponentUpdate。您可以实现自己的比较函数,也可以使用 react-addons-shallow-compare 中的比较函数。
var shallowCompare = require('react-addons-shallow-compare');
var Example = React.createClass({
shouldComponentUpdate: function(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
},
// rest of the component
});
推荐阅读
- android - 运行 Image Asset Studio 启动 Image Asset Studio - Flutter
- python - 随机森林的 TreeExplainer 的 expected_value 字段是什么?
- algorithm - 二次探测负载系数必须为 0.5?
- r - 在R中的igraph图上定位边缘标签
- blazor - 错误:连接因错误“错误:服务器在关闭时返回错误:连接因错误而关闭。”而断开连接。
- ngrx - 如何订阅和取消订阅单个 Firestore 查询?
- r - 使用 R 生成嵌套 JSON
- php - 仍然为 null symfony 的形式
- javascript - 经典表情符号 unicode 列表
- java - 理解这个数独求解器的部分内容