(編輯) 在下面的簡單示例中,我有一段時間的傳遞和訪問引用數組成員的道具的語法。反應道具語法 - 通過引用數組時遇到問題
它是如何正確地處理作爲道具傳遞的對象數組的屬性?
我想通過參考,我不明白爲什麼我不能這樣做。
感謝
// Define a type constant
const MyType {
constructor(props) {
this.state = {
panelNumber : 0,
panelDisplayMode : 'SINGLE'
}
}
}
// create a collection of those type constants
class MyStore extends React.Component{
MyCollection = [];
constructor() {
super();
}
componentWillMount(){
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
this.MyCollection.push(nonsense);
}
// pass one of the instances (array) of the collection to a component.
render() {
<div>
<MyComponent panel={this.MyCollection[2]}
</div?
}
}
// access one of the properties of the instance (array).
const MyComponent = props => (
<div>
<p>MODE:</p>{ props.panel.panelDisplayMode }
</div>
);
對不起。我重構了使用STATE到FIELD的例子。這是否澄清了這個問題?我不想複製數據,但發送參考。出於某種原因,我無法解決課程的特性。 –