首页 > 解决方案 > 在调查问卷末尾推送文件(json) - Javascript

问题描述

我有一个包含问题和答案的 json,它们分别显示在 div 中。用户可以点击一个答案,我有一个 onClick 功能可以转到下一个问题

() => setQuestionIndex(() => questionIndex + 1

我希望当 json“问题”完成后,推送一个新的 json“解决方案”:

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>
 );
});

标签: javascriptjson

解决方案


这是一个错误吗?如果是这样,这条线

if (questionIndex === question.length) {

应该:

if (questionIndex === question.length - 1) { 

还有为什么一切都是函数?


推荐阅读