首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在未使用redux验证条件时防止重新发送页面

如何在未使用redux验证条件时防止重新发送页面
EN

Stack Overflow用户
提问于 2019-10-01 20:41:04
回答 1查看 34关注 0票数 1

我有一个页面,其中包含一篇论文,其中包含一个问题和一个选项列表,以及一个重定向到下一个问题的按钮。

代码语言:javascript
复制
import Grid from "@material-ui/core/Grid";
import Typography from "@material-ui/core/Typography";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import SyntaxHighlighter from "react-syntax-highlighter";
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";
import { Dispatch } from "redux";
import { Field, reduxForm } from "redux-form";
import { incrementQuestion, IQuestion, questionRequest } from "../../actions/index";
import CheckBoxWrapper from "../../components/common/CheckBoxWrapper";
import ContentQuiz from "../../components/ContentQuiz";
import history from "../../history/history";

interface IProps {
  currentQuestionNumber: number;
  loadingData: boolean;
  questions: IQuestion[];
  questionRequest: () => void;
  incrementQuestion: () => void;
  numberOfQuestions: number;
}

const Quiz = (props: IProps) => {
  const { currentQuestionNumber,
    loadingData,
    questions,
    questionRequest,
    incrementQuestion,
    numberOfQuestions } = props;
  useEffect(() => {
    questionRequest();
  });

  const handleNextQuiz = () => {
    if (currentQuestionNumber === numberOfQuestions - 1) {
      history.push("/homepage");
    }
    incrementQuestion();
    history.push("/contentQuiz");
  };

  const currentQuestion = questions[currentQuestionNumber];
  return (
    <div>
      {loadingData ? ("Loading ...") : (
        < ContentQuiz
          questionNumber={currentQuestionNumber + 1}
          handleClick={handleNextQuiz} >
          <div>
            <Typography variant="h3" gutterBottom> What's the output of </Typography>
            <>
              <SyntaxHighlighter language="javascript" style={dark} >
                {currentQuestion.description.replace(";", "\n")}
              </SyntaxHighlighter >
              <form>
                <Grid container direction="column" alignItems="baseline">
                  {currentQuestion.options.map((option: string, index: number) => {
                    const fieldName = `option ${index + 1}`;
                    return (
                      <Grid key={index}>
                        <Field
                          name={fieldName}
                          component={CheckBoxWrapper}
                          label={option}
                        />
                      </Grid>);
                  }
                  )}
                </Grid>
              </form>
            </>
          </div >
        </ContentQuiz >
      )}
    </div>
  );
};

const mapStateToProps = (state: any) => {
  const { currentQuestionNumber, loadingData, questions, numberOfQuestions } = state.quiz;

  return {
    currentQuestionNumber,
    loadingData,
    questions,
    numberOfQuestions
  };
};

const mapDispatchToProps = (dispatch: Dispatch) => {
  return {
    incrementQuestion: () => dispatch<any>(incrementQuestion()),
    questionRequest: () => dispatch<any>(questionRequest())
  };
};

const QuizContainer = reduxForm<{}, IProps>({
  form: "Answers",
  destroyOnUnmount: false,
})(Quiz);

export default connect(mapStateToProps, mapDispatchToProps)(QuizContainer);

handleNextQuiz增加问题编号的计数器,并重定向到同一页面以重新呈现组件(我认为每次单击按钮都重定向到相同的页面不是最好的主意,任何处理建议都会受到欢迎)。但是我想检查一下,当这是最后一个问题时,用户应该被重定向到另一个页面进行验证和提交。然而,在我的代码中,当测试带有问题列表的组件时,以及当我试图将用户重定向到最终页面时。该组件已重新渲染,并出现以下错误:

代码语言:javascript
复制
TypeError: Cannot read property 'description' of undefined

我如何处理这种情况,以便在单击最后一个问题中的下一步按钮时不重新呈现同一页。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-01 21:04:13

代码语言:javascript
复制
const handleNextQuiz = () => {
    if (currentQuestionNumber === numberOfQuestions - 1) {
      history.push("/homepage");
    }
    incrementQuestion();
    history.push("/contentQuiz");
  };

上述函数不正确,假设满足第一个条件,成功重定向至/homepage,函数尚未结束。执行incrementQuestion();,然后重定向到/contentQuiz

您需要在if条件中添加一条return语句,以避免执行下面的代码,或者使用if else。所以基本上,一次应该只有一个重定向。

代码语言:javascript
复制
const handleNextQuiz = () => {
  if (currentQuestionNumber === numberOfQuestions - 1) {
    history.push("/homepage");
  } else {
    incrementQuestion();
    history.push("/contentQuiz");
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58185004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档