javascript - React:PureComponent 和组件原型
问题描述
零件:
function Component(props, context, updater) {
this.props = props;
this.context = context;
this.refs = emptyObject;
this.updater = updater || ReactNoopUpdateQueue;
}
纯组件:
function ComponentDummy() {}
ComponentDummy.prototype = Component.prototype;
function PureComponent(props, context, updater) {
this.props = props;
this.context = context;
this.refs = emptyObject;
this.updater = updater || ReactNoopUpdateQueue;
}
const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
pureComponentPrototype.constructor = PureComponent;
// Avoid an extra prototype jump for these methods.
Object.assign(pureComponentPrototype, Component.prototype);
pureComponentPrototype.isPureReactComponent = true;
我真的很困惑ComponentDummy
。为什么我们不能使用pureComponent.prototype = {}
有人能解释一下下面的代码实际上做了什么吗???
const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
pureComponentPrototype.constructor = PureComponent;
如果你能分享我的原型链,我真的很Component
Dummy
感激PureComponent
解决方案
因为如果我们使用PureComponent.prototype = new Component()
,函数 Component 会被调用,并且 props、context 和 refs 也会被创建,这会导致占用更多的存储空间。
但是如果我们使用PureComponent.prototype = new ComponentDummy()
,我们可以节省那些无用的存储空间。
我想。
推荐阅读
- python - 如何从 JSON 响应 RobotFramework 获取所需参数
- python - Plotly:如何从两个数据框列绘制两条线并从其他两列分配悬停信息?
- swift - Xcode 中的“首选大标题”是否存在错误?
- c - 无法弄清楚 C 语言中的 scanf("%[^\n]\n", input) 会发生什么
- javascript - Javascript - 构造一个数组,其中的元素从字符串中重复
- json - 从服务器获取 Flutter 和 JSON 的问题
- postgresql - Postgres - 按特定条件排序
- javascript - 如何在 javascript 或 typescript 中将时间戳转换为人类日期
- angular - 带有路径的表单控件没有值访问器:'funder_details -> 0。生成动态表单数组时出现此错误
- node.js - $add,$subtract MongoDB 中的聚合框架