我正在开发一款类似于测验应用的程序,这是应用刚启动时的初始状态。我还有一个名为quizApplication.js的组件,用于存储所有问题和答案。
{
question: "I am task oriented in order to achieve certain goals",
answers: [
{
type: "Brown,D,JP",
content: "Hell Ya!"
},
{
type: " ",
content: "Nah"
}
]
},
这是我的设置用户答案的函数:
setUserAnswer(answer) {
if (answer.trim()) {
const answer_array = answer.split(',');
const updatedAnswersCount = update(this.state.answersCount, {
[answer]: {$apply: (currentValue) => currentValue + 1},
});
this.setState({
answersCount: updatedAnswersCount,
answer: answer
});
}
}
我也有一个AnswerOption组件,如下所示:
function AnswerOption(props) {
return (
<AnswerOptionLi>
<Input
checked={props.answerType === props.answer}
id={props.answerType}
value={props.answerType}
disabled={props.answer}
onChange={props.onAnswerSelected}
/>
<Label className="radioCustomLabel" htmlFor={props.answerType}>
{props.answerContent}
</Label>
</AnswerOptionLi>
);
}
我要做的是每当用户点击“HellYa!”时,就会将“Brown”、“D”和“JP”增加1,但是现在它给我一个新的answersCount值,即Brown、D、JP:null,那么我应该如何实现呢?非常感谢!