【发布时间】:2016-02-16 16:02:41
【问题描述】:
这是我想做的:
constructor(props, store) {
props.store = store;
super(props);
};
我收到以下错误:
Uncaught TypeError: Can't add property store, object is not extensible
我了解 ReactJS 中的属性是不可变的。那么如何克隆,然后扩充现有属性?
更新 1
一些背景知识:我基本上是想避免this._store“私有”变量,而宁愿将它放在props 中,因为该值在对象创建时是已知的并且不会改变。
我的类层次结构如下:
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);
};
...
};
【问题讨论】:
-
这里的场景是什么?我不认为你可以在 React 中做这样的事情。但我可以想象有办法解决这个问题。
-
为什么不将 store 作为父组件或 React.createElement 调用的另一个 prop 传递?
标签: reactjs ecmascript-6