首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onChange不能在radioGroup中用资料用户界面触发。

onChange不能在radioGroup中用资料用户界面触发。
EN

Stack Overflow用户
提问于 2019-10-06 15:22:05
回答 1查看 1.5K关注 0票数 2

我使用RadioButton组件在我创建的组件中显示问题的不同选项:

代码语言:javascript
复制
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Grid from "@material-ui/core/Grid";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup/RadioGroup";
import Typography from "@material-ui/core/Typography";
import React, { useEffect, useState } 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 { incrementQuestion, IQuestion, questionRequest } from "../../actions/index";
import ContentQuiz from "../../components/ContentQuiz";
import history from "../../history/history";

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

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

  const [answerOption, setAnswerOption] = useState(1);

  useEffect(() => {
    questionRequest();
  }, [questionRequest]);

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setAnswerOption(Number((event.target as HTMLInputElement).value));
  };
  const handleNextQuiz = () => {
    if (currentQuestionNumber === numberOfQuestions - 1) {
      history.push("/homepage");
    } else {
      incrementQuestion(answerOption);
      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 >
                <Grid container direction="column" alignItems="baseline">
                  <Grid>
                    <FormControl component="fieldset">
                      <RadioGroup
                        name="option"
                        value={answerOption}
                        onChange={handleChange}>
                        {currentQuestion.options.map((option: string, index: number) => (
                          <FormControlLabel
                            key={index}
                            control={<Radio color="secondary" />}
                            label={option}
                            value={index + 1}
                            labelPlacement="end"
                          />))
                        }
                      </RadioGroup>
                    </FormControl>
                  </Grid>
                </Grid>
            </>
          </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: (answer: any) => dispatch<any>(incrementQuestion(answer)),
    questionRequest: () => dispatch<any>(questionRequest())
  };
};

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

但是,可以使用此代码检查不同的选项,根据单选按钮,正常行为只能检查一个选项。我找到了一个类似的问题,并试图根据答案更新我的代码,但是它不起作用,因为在我的例子中,handleChange函数没有被调用,我不知道为什么?

EN

回答 1

Stack Overflow用户

发布于 2019-11-22 09:38:05

正如我从您提供的链接中了解到的,您可以尝试以下操作:

  1. 增加以下功能部分: const FormControlLabelWrapper = props => { const { index,...labelProps }= props;返回(
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58258667

复制
相关文章

相似问题

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