【发布时间】:2019-06-03 10:12:42
【问题描述】:
App
|
|----|---------------------|
| |
RadioInputForm TextInputForm
我正在尝试使用来自子组件的数据更新父状态中的数组,但事件未触发。我已经尝试过以前的解决方案,例如 {() => callback()} 和 {() => callback.bind(this)},但似乎没有任何效果。
在指定索引处输入值后,应更新处于父状态的数组。这将如何工作类似于调查网络应用程序,您应该能够返回到以前的条目,因此是数组。
这是我到目前为止所尝试的,任何帮助将不胜感激:
// App.js
constructor(props) {
super(props);
let radioQuestions = [], textQuestions = [];
let option = {id: "", options: {option1: false, option2: false, option3: false}, text: ""};
for(var i = 0; i < data.questions.length; ++i) {
textQuestions[i] = "";
if(data.questions[i].question_type === "RadioQuestion") {
radioQuestions[i] = option;
radioQuestions[i].id = data.questions[i].id;
} else radioQuestions[i] = {};
}
this.state = {
textQuestions: textQuestions,
radioQuestions: radioQuestions,
index: 0
};
this.handleRadioSelect= this.handleRadioSelect.bind(this);
this.handleRadioTextChange = this.handleRadioTextChange.bind(this);
this.handleTextQuestionChange= this.handleTextQuestionChange.bind(this);
this.handleBack = this.handleBack.bind(this);
this.handleNext = this.handleNext.bind(this);
}
handleRadioSelect(e) {
const { value } = e.target;
const questions = this.state.radioQuestions.slice();
const index = this.state.index;
switch(value) {
case "option1":
if(questions[index].options.option1 === true)
questions[index].options.option1 = false;
else questions[index].options.option1 = true;
break;
case "option2":
if(questions[index].options.option2 === true)
questions[index].options.option2 = false;
else questions[index].options.option2 = true;
break;
case "option3":
if(questions[index].options.option3 === true)
questions[index].options.option3 = false;
else questions[index].options.option3 = true;
break;
default:
}
this.setState({
radioQuestions: questions
});
}
handleRadioTextChange(e) {
const { value } = e.target;
const questions = this.state.radioQuestions.slice()
questions[this.state.index].text = value;
this.setState({
radioQuestions: questions
})
}
handleTextQuestionChange(e) {
const { value } = e.target;
const answers = this.state.textAnswers.slice()
answers[this.props.index] = value;
this.setState({
textAnswers: answers
})
}
带有 Radio 表单的子组件:
constructor(props) {
super(props);
this.state = {
radioQuestions: this.props.radioQuestions
}
this.handleRadioTextChange = this.handleRadioTextChange.bind(this);
this.handleRadioSelect = this.handleRadioSelect.bind(this);
}
handleRadioSelect(e) {
const { value } = e.target;
const questions = this.state.radioQuestions.slice();
const index = this.state.index;
console.log(JSON.stringify(questions[index]));
switch(value) {
case "option1":
if(questions[index].options.option1 === true)
questions[index].options.option1 = false;
else questions[index].options.option1 = true;
break;
case "option2":
if(questions[index].options.option2 === true)
questions[index].options.option2 = false;
else questions[index].options.option2 = true;
break;
case "option3":
if(questions[index].options.option3 === true)
questions[index].options.option3 = false;
else questions[index].options.option3 = true;
break;
default:
}
this.props.handleRadioSelect(e);
this.setState({
radioQuestions: questions
});
}
handleRadioTextChange(e) {
const { value } = e.target;
const questions = this.state.radioQuestions.slice()
questions[this.props.index].text = value;
this.setState({
radioQuestions: questions
})
}
render() {
return (
<div className="form-group">
<input type="radio" name="option1"
value="option1"
onChange = { this.state.handleRadioSelect }
defaultChecked={ this.state.radioQuestions[this.props.index].options.option1 } /> Option 1<br />
<input type="radio" name="option2"
value="option2"
onChange = { this.state.handleRadioSelect }
defaultChecked={ this.state.radioQuestions[this.props.index].options.option2 } /> Option 2<br />
<input type="radio" name="option3"
value="option3"
onChange = { this.state.handleRadioSelect }
defaultChecked={ this.state.radioQuestions[this.props.index].options.option3 } /> Option 3<br />
<textarea rows="1" cols="40" id="answer" name="answer"
className ="form-control input-default"
onChange={ this.state.handleRadioTextChange }
defaultValue = { this.state.radioQuestions[this.props.index].text } />
</div>
);
}
重复代码(在父子代码中)来自我解决问题的尝试。最后是文本输入表单组件:
constructor(props) {
super(props);
this.state = {
textAnswers: this.props.textQuestions,
};
this.handleTextQuestionChange = this.handleTextQuestionChange.bind(this);
}
handleTextQuestionChange(e) {
const { value } = e.target;
const answers = this.state.textAnswers.slice()
answers[this.props.index] = value;
this.props.handleTextQuestionChange(e);
this.setState({
textAnswers: answers
})
}
render() {
return (
<div className="form-group">
<textarea rows="4" cols="50" id="answer" name="answer"
className ="form-control input-default" onChange = { this.state.handleTextQuestionChange }
defaultValue = { this.state.textAnswers[this.props.index] } />
</div>
);
}
如果我没有遵循良好的做法,如果我有任何额外的建议,我也将不胜感激,谢谢。
【问题讨论】:
-
我想我看到了一个解决方案,而且看起来很简单,但是你可以制作一个 sn-p 或 CodePen 以使其更易于使用吗?
-
另外,@Mark20,你能把
App组件的render方法贴在这里吗? -
@0xc14m1z 这是一个代码框链接:codesandbox.io/s/4x5x6oo054
-
谢谢@Mark20,我分叉了codesandbox并正在处理它。希望你不介意一点重构:)
标签: reactjs react-native ecmascript-6