单选按钮未根据 React 中 Select 选项的更改正确更新

Radio button not updating correctly based on change in Select option in React

我有一个包含 2 个选项的 Select 下拉菜单和一个包含 2 个值的单选按钮。 (参考下面的代码)加载页面时,“象限 1”是默认 Select 选项,单选按钮的 none 将默认 select。

对于 select 选项的“象限 1”,我将 select 单选按钮中的“否”。对于 select 选项的“象限 2”,我会 select “是”。这些值对应地存储在“Grid”状态中。每当进行更改时,我都能够成功捕获状态中的这些更新。

但是当我在select选项之间切换时,单选按钮并没有相应改变。我知道只要更改 Select 选项,我就必须更新单选按钮的“选中”属性。但我不能那样做。如果我错了,请原谅我,因为我是 ReactJS 的新手,并且在过去的 1 周里撞到了我的头。

每当 select 选项发生变化时,请帮助我更新单选按钮。

Updated RuleScreen.js:

[Correct Grid State details but radio button not updated][1]
import React, { Component } from "react";
import M from "materialize-css";
import dropDownList from "./dropDown";
import _ from "lodash";

class RuleScreen extends Component {

  constructor(props){
    super(props);
    this.state = {
      quad: "Q1",
      grid: [{id:"Q1", optin:"",NL:""}, {id:"Q2", optin:"", NL:""}]
    };
  }

  componentDidMount() {
    var elems = document.querySelectorAll('select');
    M.FormSelect.init(elems, {hover: true, coverTrigger: false});
  }

  quadChange = async (e) => {
    await this.setState({ quad:e.target.value});
  }

  optinChange = async (e) => {
    const currQuad = this.state.quad;
    const value = e.target.value;
    await this.setState(prevState => ({
      grid: prevState.grid.map(
        item => item.id === currQuad? { ...item, optin: value}: item
      )
    }))
  }

  render() {
    const selectedGrid = this.state.grid.filter(quad=>quad.id === this.state.quad)
    return (
      <div>
        <h3>Rule Setup</h3>
        <div className="container">
          <div className="btn">
            <select required value={this.state.quad} onChange={this.quadChange}>
              <option key="Q1" value="Q1">Quadrant 1</option>
              <option key="Q2" value="Q2">Quadrant 2</option>
            </select>
          </div>
          <form>
            <label>Opt In</label>
            <label><input id="yes" checked={selectedGrid[0].optin=== 'yes' } onChange={this.optinChange} value="yes" className="with-gap" name="optin" type="radio"/>
                <span>Yes</span>
            </label>
            <label><input id="no" checked={selectedGrid[0].optin=== 'no' } onChange={this.optinChange} value="no" className="with-gap" name="optin" type="radio"/>
                <span>No</span>
            </label>
          </form>
        </div>
      </div>
    );
  }
}


export default RuleScreen;

由于您使用数组函数,因此不必将函数绑定到 'this'。 我还没有测试代码,但想法就在这里。 将值添加到单选按钮,删除值的 getter,将选择器添加到渲染中的网格

class RuleScreen extends Component {

      constructor(props){
        super(props);
        this.state = {
          quad: "Q1",
          grid: [{id:"Q1", optin:"", NL:""}, {id:"Q2", optin:"", NL:""}]
        };
      }

      componentDidMount() {
        var elems = document.querySelectorAll('select');
        M.FormSelect.init(elems, {hover: true, coverTrigger: false});
      }

      quadChange = (e) => {
        this.setState({ quad:e.target.value});
      }

      optinChange = (e) => {
        const currQuad = this.state.quad;
        const value = e.target.value;
        this.setState(prevState => ({
          grid: prevState.grid.map(
            item => item.id === currQuad? { ...item, optin: value}: item
          )
        }))
      }
      

      render() {
        const selectedGrid = this.state.grid.filter(quad=>quad.id === this.state.quad) //maybe with [0] at the end
        return (
          <div>
            <h3>App Name</h3>
            <div className="container">
              <div className="btn">
                <select required value={this.state.quad} onChange={this.quadChange}>
                 <option key="Q1" value="Q1">Quadrant 1</option>
                 <option key="Q2" value="Q2">Quadrant 2</option>
                </select>
              </div>
              <form>
                <div>Opt In</div>
                <label>
                  <input id="yes" checked={selectedGrid.optin === 'yes'} onChange={this.optinChange} value="yes" className="with-gap" name="optin" type="radio"/>
                    <span>Yes</span>
                </label>
                <label>
                  <input id="no" checked={selectedGrid.optin === 'no'} onChange={this.optinChange} value="no" className="with-gap" name="optin" type="radio"/>
                    <span>No</span>
                </label>
              </form>
          </div>
        )
      }
}


export default RuleScreen;