如何获取 Reactstrap 的 CustomInput Switch 组件的状态,以及如何从数组映射开关?

Bla*_*Zay 3 frontend twitter-bootstrap reactjs bootstrap-4 reactstrap

        <FormGroup>
          <div>
            {this.props.diseases.map((disease, index) => (
              <FormGroup>
                <CustomInput
                  type="switch" 
                  id="exampleCustomSwitch"
                  key={disease}
                  disease={disease}
                  onClick={(disease) => this.props.toggle(disease)}
                  label={disease}
                />
              </FormGroup>
            ))
            }
          </div>
        </FormGroup>
Run Code Online (Sandbox Code Playgroud)
  1. 我希望能够找出开关的状态,无论是打开还是关闭。不知道我怎样才能做到这一点?我是否要传递某种默认值,其中 0 表示关闭,1 表示打开?

  2. 目前,开关已从阵列中正确映射,但打开或关闭仅适用于第一个开关。因此,如果我单击任何其他开关,由于某种原因,第一个开关会切换。

Akb*_*bal 5

对于点#1,y您可以用来e.target.checked检查特定的真/假状态CustomInput;检查这个 stackblitz看看它是否有效

对于第 2 点,如果您共享现有代码,将更容易帮助您解决特定场景

相关js

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: "World to React",
      log: []
    };
    this.customInputSwitched.bind(this);
  }

  customInputSwitched(buttonName, e) {
    let newStr = `we received ${e.target.checked} for ${buttonName}...`;
    console.log(newStr);
    let newLog = [...this.state.log, newStr];
    this.setState({ log: newLog });
  }

  render() {
    var testName = "modal for testing - click here";
    return (
      <div>
        <Hello name={this.state.name} />
        <p>Start editing to see some magic happen :)</p>
        <Form>
          <FormGroup>
            <Label for="exampleCheckbox">Switches</Label>
            <div>
              <CustomInput
                type="switch"
                id="exampleCustomSwitch"
                name="customSwitch"
                label="Turn on this custom switch"
                onChange={this.customInputSwitched.bind(this, "button1")}
              />
              <CustomInput
                type="switch"
                id="exampleCustomSwitch2"
                name="customSwitch"
                label="Or this one"
                onChange={this.customInputSwitched.bind(this, "button2")}
              />
              <CustomInput
                type="switch"
                id="exampleCustomSwitch3"
                label="But not this disabled one"
                disabled
              />
              <CustomInput
                type="switch"
                id="exampleCustomSwitch4"
                label="Can't click this label to turn on!"
                htmlFor="exampleCustomSwitch4_X"
                disabled
              />
            </div>
          </FormGroup>
        </Form>
        {this.state.log}
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

更新#1:根据下面提问者的评论

https://stackblitz.com/edit/react-rcqlwq的代码中几乎没有问题

  • 您必须在构造函数中实例化 Log 状态
  • customInputSwitched函数应该传递特定按钮的参数,而不是硬编码的“button1” - 因此我们添加疾病的索引号
  • 所有按钮的ID不能是相同的'exampleCustomSwitch',所以我们只需将索引号添加到ID
  • 映射为数组的最佳实践是还包含索引,这有好处(如接下来的两点所示)

与您的代码/stackblitz相关的工作 JS

class App extends Component {
  constructor() {
    super();
    this.state = {
      diseases: [
  "Normal",
  "Over inflated lungs",
  "Pneumonia",
  "Pneumothorax",
  "Congestive cardiac failure",
  "Consolidation",
  "Hilar enlargement",
  "Medical device",
  "Effusion"
],
log: []
    };
    this.customInputSwitched.bind(this);
  }

  customInputSwitched(buttonName, e) {
    let newStr = `we received ${e.target.checked} for ${buttonName}...`;
    console.log(newStr);
    let newLog = [...this.state.log, newStr];
    this.setState({ log: newLog });
  }

  render() {
    return (
      <div>
        <p>Start editing to see some magic happen :)</p>
        <Form>
          <FormGroup>
            <Label for="exampleCheckbox">Switches</Label>
            {this.state.diseases.map((disease, index) => {
              //console.log(disease, index);
              let idName = "exampleCustomSwitch"+index;

              return (
              <div key={index}>
                <CustomInput
                type="switch"
                id={idName}
                name="customSwitch"
                label={disease}
                onChange={this.customInputSwitched.bind(this, "button"+index)}
              />
              </div>
              );
            }

            )}
          </FormGroup>
        </Form>
        {this.state.log}
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)