我有一个包含问题和答案的json,它们分别在div中显示。用户可以单击一个答案,而我有一个onClick功能,可以转到下一个问题
() => setQuestionIndex(() => questionIndex + 1
我希望当json“问题”完成时,会推送一个新的json“ solutions”:
onAnswer={() => {
if (questionIndex === question.length) {
() => history.push('/forms/solutions')
} else {
() => setQuestionIndex(() => questionIndex + 1)
}
}}
当我只使用() => setQuestionIndex(() => questionIndex + 1
时,它会完美地跳到下一个问题,但是条件语句不起作用,有任何猜测吗?
完整代码:
const Form = enhance(({
styles, scenario, questionIndex, setQuestionIndex
}) => {
const { questions } = scenarios[scenario];
const questionTabs = questions.map((q, i) => {
if (i === questionIndex) {
return <Tab key={i} {...css(styles.tabActive)}> {i} </Tab>
}
return <Tab key={i} {...css(styles.tab)}> {i} </Tab>
})
const questionPanels = questions.map((question, i, history) => {
return (
<TabPanel key={i}>
<Question
question={question.question}
answers={question.answers}
visualAnalogScale={question.VAS}
//onAnswer={() => setQuestionIndex(() => questionIndex + 1)}
onAnswer={() => {
if (questionIndex === question.length) {
() => history.push('/forms/solutions')
} else {
() => setQuestionIndex(() => questionIndex + 1)
}
}}
/>
</TabPanel>
);
});
答案 0 :(得分:0)
一次出错吗?如果是这样,这行
if (questionIndex === question.length) {
应该是:
if (questionIndex === question.length - 1) {
为什么所有东西都是函数?