简体   繁体   English

React:PureComponent 和组件原型

[英]React: PureComponent and Component prototype

Component:零件:

function Component(props, context, updater) {
  this.props = props;
  this.context = context;
  this.refs = emptyObject;
  this.updater = updater || ReactNoopUpdateQueue;
}

PureComponent:纯组件:

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;

I'm really comfused about the ComponentDummy .我真的对ComponentDummy感到困惑。 Why could't us use pureComponent.prototype = {} Could someone explain what the codes below actually do???为什么我们不能使用pureComponent.prototype = {}有人能解释一下下面的代码实际上做了什么吗???

const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
pureComponentPrototype.constructor = PureComponent;

I really appreciate if you can share me the prototype chain about Component Dummy and PureComponent如果你能分享我关于Component DummyPureComponent的原型链,我真的很感激

Because If We use PureComponent.prototype = new Component() , the function Component will be called, and the props, context, and refs will also be created, which can be lead to take more storage.因为如果我们使用PureComponent.prototype = new Component() ,则会调用 function 组件,同时还会创建 props、context 和 refs,这会导致占用更多存储空间。

But if we use PureComponent.prototype = new ComponentDummy() , we can save that useless storage.但是如果我们使用PureComponent.prototype = new ComponentDummy() ,我们可以节省那些无用的存储空间。

I thought.我想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM