【问题标题】:I want to implement a conditional rendering system in reactjs我想在 reactjs 中实现一个条件渲染系统
【发布时间】:2020-07-15 15:05:11
【问题描述】:

我想在渲染数据库中的数据时显示所选选中按钮的值是否与数据库中的答案相同,我有一个名为的变量,它具有正确答案的值。

另外,我可以获取所选单选按钮的值。我想根据单选值是否与正确答案相同来渲染两个 div,但我不明白该怎么做。

import React, { Component } from 'react';
import axios from 'axios'
import {Form,Radio,} from 'semantic-ui-react'
import 'semantic-ui-css/semantic.min.css';





export default class QuestionList extends Component {
    constructor(props){
        super(props)
        this.handleOptionChange = this.handleOptionChange.bind(this);
        this.handleFormSubmit = this.handleFormSubmit.bind(this);

        this.state = {questions:[],
          answer:''}

    }

    handleOptionChange(e){
      this.setState({
        answer: e.target.value
      });
    }

    handleFormSubmit (e) {
      e.preventDefault();

      console.log('You have selected:', this.state.answer);
    }


    componentDidMount(){
        axios.get('http://localhost:5000/question/')
        .then(respose =>{
            this.setState({questions:respose.data})
        })
        .catch(error => {
            console.log(error);
        });

    };



    questionList() {

        return this.state.questions.map(currentquestion => {

          return(
            <Form onSubmit ={this.handleFormSubmit}>
        <Form.Field>
         <h3>Q.  {currentquestion.ques}</h3>
          <h6>Correct Answer is {currentquestion.ans} </h6>
        </Form.Field>
        <Form.Field>
          <Radio
            label={currentquestion.options[0]}
            name='radioGroup'
            value ={currentquestion.options[0]}
            checked = {this.state.answer === currentquestion.options[0]}
            onChange={this.handleOptionChange}

          />
        </Form.Field>
        <Form.Field>
          <Radio
            label={currentquestion.options[1]}
            name='radioGroup'
            value ={currentquestion.options[1]}
            checked = {this.state.answer === currentquestion.options[1]}
            onChange={this.handleOptionChange}

          />
        </Form.Field>
        <Form.Field>
          <Radio
            label={currentquestion.options[2]}
            name='radioGroup'
            value = {currentquestion.options[2]}
            checked = {this.state.answer === currentquestion.options[2]}
            onChange={this.handleOptionChange}

          />
        </Form.Field>
        <Form.Field>
          <Radio
            label={currentquestion.options[3]}
            name='radioGroup'
            value={currentquestion.options[3]}
            checked = {this.state.answer === currentquestion.options[3]}
            onChange={this.handleOptionChange}

          />
        </Form.Field>

        <button type ="submit" class="ui button" >Submit Answer</button>
        <hr />

      </Form>

          );
        })
      }



  render() {
    return (
        <div>
        <h2>Questions</h2>
        <hr />
        <hr />
        <p>{ this.questionList() } </p>

      </div>
    )
  }

但是当我尝试选择单选按钮时,我无法检查它们,这意味着我无法存储任何处于应答状态的内容。

【问题讨论】:

  • 试试this.state.answer == currentquestion.options[i],只有2个=

标签: javascript reactjs react-redux conditional-rendering


【解决方案1】:

semantic-ui 中,onChange 有两个参数,(e, value),如果你在handlechange 中找到console.log 值,你会在那里找到它,所以这是你应该在你的状态中输入的值,您可以在documentation 的操场内尝试:https://react.semantic-ui.com/collections/form/#shorthand-field-control 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 2022-01-15
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多