【发布时间】:2019-01-09 04:01:51
【问题描述】:
我正在从父组件中的数组加载数据并将其多次传递给子组件。我注意到数组中的第一个项目被发送给孩子,但之后其余的组件没有被孩子传递/接收。这是我用来访问数组的代码示例:
{(() => {
switch(data.questions[this.state.index].question_type) {
case "RadioQuestion": console.log(JSON.stringify(this.state.radioQuestions[this.state.index]));
return <RadioQuestion
radioQuestion = { this.state.radioQuestions[this.state.index] }
handleRadio = { this.handleRadio } />;
case "TextQuestion": console.log(JSON.stringify(this.state.textQuestions[this.state.index]));
return <TextQuestion
textQuestion = { this.state.textQuestions[this.state.index] }
handleText = { this.handleText } />;
case "FileUpload": return <FileUpload index = { this.state.index } />;
default: return <h2>Unknown Question Format</h2>
}
})()}
我已记录 (console.log()) 来自上方各个数组的数据,正确的项目正在使用索引从数组中加载。
在各自的子元素中,添加第一个元素后状态似乎没有更新:
// Radio Component
constructor(props) {
super(props);
this.state = {
radioQuestion: this.props.radioQuestion
};
this.handleRadioText = this.handleRadioText.bind(this);
this.handleRadioSelect = this.handleRadioSelect.bind(this);
}
// Text Component
constructor(props) {
super(props);
this.state = {
textQuestion: this.props.textQuestion
};
this.handleTextChange = this.handleTextChange.bind(this);
}
我有什么遗漏或做错了吗?非常感谢您的反馈,谢谢。
【问题讨论】:
-
嗨@Mark20 我检查了你的沙箱codesandbox.io/s/4x5x6oo054,能帮我理解你的问题吗?我不清楚
-
@PraveenRaoChavan.G 解决了谢谢,提供一些帮助
标签: reactjs react-native ecmascript-6