Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ReactJS-ES6 class-call super()和扩展属性_Reactjs_Ecmascript 6 - Fatal编程技术网

ReactJS-ES6 class-call super()和扩展属性

ReactJS-ES6 class-call super()和扩展属性,reactjs,ecmascript-6,Reactjs,Ecmascript 6,这就是我想做的: constructor(props, store) { props.store = store; super(props); }; 我得到以下错误: 未捕获的TypeError:无法添加属性存储,对象不可扩展 我知道属性在ReactJS中是不可变的。那么我如何克隆,然后扩展现有的属性呢 更新1 一些背景:我基本上是在试图避免使用这个变量。_store“private”变量,而宁愿将它放在道具中,因为该值在对象创建时已知,不会更改 我的类层次结构如下所示: import

这就是我想做的:

constructor(props, store) {
  props.store = store;
  super(props);
};
我得到以下错误:

未捕获的TypeError:无法添加属性存储,对象不可扩展

我知道属性在ReactJS中是不可变的。那么我如何克隆,然后扩展现有的属性呢

更新1

一些背景:我基本上是在试图避免使用
这个变量。_store
“private”变量,而宁愿将它放在
道具中,因为该值在对象创建时已知,不会更改

我的类层次结构如下所示:

import SpeakersStore from ...;
class SpeakersViewController extends ItemsViewController {
  constructor(props) {
    super(props, SpeakersStore);
  };
  ...
};

class ItemsViewController extends React.Component {
  constructor(props, store) {
    props.store = store;
    super(props);
  };
  ...
};

您应该能够执行以下操作:

constructor(props, store) {
  super({...props, store});
}
如果您所处的环境中没有对象扩展运算符(
{…props}
构造),则可以使用
对象。分配

constructor(props, store) {
  super(Object.assign({}, props, {store});
}

但是,如果这是React组件的构造函数,请注意
React.component
的构造函数的第二个参数是
context
,您将无法获得所需的行为。

这里的场景是什么?我不认为你能做这样的事。但是我可以想象有一种方法可以解决这个问题。为什么不从父组件或React.createElement调用中将存储作为另一个道具传递?最好使用
Object.assign
,因为这个问题没有提到ES7或Babel。我使用的是Babel,所以不用担心传播。。。你的代码很有道理,看起来应该可以用,但我还是用了同样的道具,也就是说,没有我的额外商店。到时候我需要更多的信息。您的类是如何构造的?它是一种化学成分吗?(如果是这样,请参阅上文最后一段)。商店是如何建造的?