javascript - 与 Axios react : Questionn with its choices in the same page

标签 javascript reactjs axios

我正在使用 React 进行测验。我想在同一页面中显示每个问题及其选择。单击按钮后,我想转到另一个页面,其中有第二个问题及其选择。它会引发错误

const {this.props.question, this.props.choice} = questions[currentQuestionIndex]

How can I fix it?

      import React from "react"
      import axios from "axios/index";

        class QuizAppQuestion extends React.Component {
            constructor(props, context) {
                super(props, context);
                this.state = {
                    currentQuestionIndex: 0,
                    questions: [],
                    answers: []
                };
            }

            componentDidMount() {
                axios
                    .get(
                        "https://private-anon-c06008d89c-quizmasters.apiary-mock.com/questions"
                    )
                    .then(response => {
                        this.setState({questions: response.data});
                    });
            }

            onChangeOption(value) {
                const {currentQuestionIndex} = this.state;
                let answers = {...this.state.answers};
                answers[currentQuestionIndex] = value;
                this.setState({answers});
            }

            handleNext() {
                let incrementCurrentQuestionIndex = this.state.currentQuestionIndex + 1;
                this.setState({currentQuestionIndex: incrementCurrentQuestionIndex});
            }

            render() {
                const {questions, currentQuestionIndex, answers} = this.state;
                if (!questions.length) {
                    return <div>Loading questions...</div>
                }
                if (currentQuestionIndex >= questions.length) {
                    return (<div><h3>End of the quiz</h3></div>)
                }

                    const {this.props.question, this.props.choice} = questions[currentQuestionIndex];


                return (
                    <div className="Question">
                        <h1>Question {currentQuestionIndex + 1}</h1>
                        <h4> {this.props.question}</h4>

                        {this.props.choices.map((c, i) => (
                            <label>
                                <input type="radio" checked={answers[currentQuestionIndex] === c.choice}
                                       onChange={(evt) => this.onChangeOption(evt.target.value)}>{c.choice}</input>
                            </label>
                        ))}

                        <button disabled={currentQuestionIndex === questions.length - 1}
                                onClick={() => this.handleNext()}>Next
                        </button>
                    </div>
                );
            }
        }

        export default QuizAppQuestion

最佳答案

我想也许你不知道reactJs中的'this.props'是什么,也许你需要将代码更改为:

const { 问题,选择 } = 问题[当前问题索引];

    return (
        <div className="Question">
            <h1>Question {currentQuestionIndex + 1}</h1>
            <h4> {question}</h4>

            {choices.map((c, i) => (
                <label>
                    <input type="radio" checked={answers[currentQuestionIndex] === c.choice}
                        onChange={(evt) => this.onChangeOption(evt.target.value)}>{c.choice}</input>
                </label>
            ))}

            <button disabled={currentQuestionIndex === questions.length - 1}
                onClick={() => this.handleNext()}>Next
              </button>
        </div>
    );

关于javascript - 与 Axios react : Questionn with its choices in the same page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51907457/

相关文章:

java - dojo.destroy 之后 Dijit 表单字段未在 dojo.place 上进行解析

javascript - 是否有适用于 Node.js/JavaScript 的 Thrift 或 Cassandra 客户端

javascript - 无服务器/AWS API Gateway CORS 无法访问 header

json - Vue Axios CORS 策略 : No 'Access-Control-Allow-Origin'

Javascript 函数在 IE11 中未定义

javascript - ES 模块导入不起作用

reactjs - 类型错误 : Cannot read property 'getState' of null

javascript - React 是否去掉了关键属性

reactjs - Mobx 状态树中的通用模型?

node.js - 从 axios 获取对象数组并使用返回值调用另一个 axios 函数并将输出附加到第一个结果