我有一个页面,其中包含一篇论文,其中包含一个问题和一个选项列表,以及一个重定向到下一个问题的按钮。
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增加问题编号的计数器,并重定向到同一页面以重新呈现组件(我认为每次单击按钮都重定向到相同的页面不是最好的主意,任何处理建议都会受到欢迎)。但是我想检查一下,当这是最后一个问题时,用户应该被重定向到另一个页面进行验证和提交。然而,在我的代码中,当测试带有问题列表的组件时,以及当我试图将用户重定向到最终页面时。该组件已重新渲染,并出现以下错误:
TypeError: Cannot read property 'description' of undefined我如何处理这种情况,以便在单击最后一个问题中的下一步按钮时不重新呈现同一页。
发布于 2019-10-01 21:04:13
const handleNextQuiz = () => {
if (currentQuestionNumber === numberOfQuestions - 1) {
history.push("/homepage");
}
incrementQuestion();
history.push("/contentQuiz");
};上述函数不正确,假设满足第一个条件,成功重定向至/homepage,函数尚未结束。执行incrementQuestion();,然后重定向到/contentQuiz。
您需要在if条件中添加一条return语句,以避免执行下面的代码,或者使用if else。所以基本上,一次应该只有一个重定向。
const handleNextQuiz = () => {
if (currentQuestionNumber === numberOfQuestions - 1) {
history.push("/homepage");
} else {
incrementQuestion();
history.push("/contentQuiz");
}
};https://stackoverflow.com/questions/58185004
复制相似问题