简体   繁体   English

在 React.js 中重置 RadioGroup 值

[英]Resetting RadioGroup value in React.js

I am attempting to build a quiz app using React.我正在尝试使用 React 构建一个测验应用程序。 I am using Material-UI for radio button input for multi-choice questions, and am having issues clearing the selected buttons when a new questions loads.我正在使用 Material-UI 输入多选问题的单选按钮,并且在加载新问题时遇到清除所选按钮的问题。 For instance, on question 1 if the user selects answer A, when they move on the the next question A is selected by default.例如,在问题 1 上,如果用户选择答案 A,则当他们继续下一个问题时,默认选择 A。

I have tried resetting this.state.value to undefined and null after a response is submitted, tried making separate state for each question to use as the starting value, etc. Nothing works.我尝试在提交响应后将this.state.value重置为 undefined 和 null,尝试为每个问题单独设置 state 以用作起始值等。 From examining the React components via developer tools I can see the value within the RadioGroup being reset to null or undefined, but Hook shows the prior answer is maintained from the question before.通过开发人员工具检查 React 组件,我可以看到 RadioGroup 中的值被重置为 null 或未定义,但 Hook 显示先前的答案保留了之前的问题。 I can't figure out how to reset.我不知道如何重置。

Here are my handlers/state:这是我的处理程序/状态:

  this.state = {
      q_number: 1,
      value: undefined
    };

    this.handleNext = this.handleNext.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  handleNext = () => {
    var new_index = this.state.q_number; 
    this.setState({ q_number: new_index + 1, value: undefined});
  }

  handleChange = (event) => {
    this.setState({ value: event.target.value }); 
  }; 

Example form with two questions:带有两个问题的示例表格:

render() {
   let content; 
   if (this.state.q_number === 1) {
        content = (
            <div>
                <FormControl className="answers-form" component="fieldset">
                    <RadioGroup name="questions1" value={this.state.value} onChange={this.handleChange}>
                        <FormControlLabel value="a" control={<Radio color="default"/>} label="Answer 1" />
                        <FormControlLabel value="b" control={<Radio color="default"/>} label="Answer 2" />

                    </RadioGroup>
                </FormControl>
            </div>
        ); 
    } else if(this.state.q_number === 2) {
        content = (
            <div>

                <FormControl className="answers-form" component="fieldset">
                    <RadioGroup name="questions2" value={this.state.value} onChange={this.handleChange}>
                        <FormControlLabel value="a" control={<Radio color="default"/>} label="Answer 1" />
                        <FormControlLabel value="b" control={<Radio color="default"/>} label="Answer 2" />
                    </RadioGroup>
                </FormControl>
            </div>
        );
    }

 return (
        <div className="body questions">
            <div>
                {content}
                <Button className={btnClass} color="primary" onClick = {() =>this.handleNext()}>
                    Next
                </Button>
            </div>
        </div>
    );

Im not seeing increment_q function, but what you want to achieve is the basic state case.我没有看到 increment_q function,但你想要实现的是基本的 state 案例。

Do something like this:做这样的事情:

<Button className={btnClass} color="primary" onClick={this.incrementNextValue} />

incrementNextValue = () => {
  this.setState({q_number: this.state.q_number + 1});
}

The same works for the handleChange in the radio buttons.单选按钮中的 handleChange 也是如此。 Also, bind is not necessary if you are using arrow functions.此外,如果您使用箭头函数,则不需要绑定。

That will re-render your component, your state will be different and the correct next button should be displayed.这将重新渲染您的组件,您的 state 将有所不同,并且应该显示正确的下一步按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM