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

标签 reactjs twitter-bootstrap bootstrap-4 frontend 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>
  1. 我想知道开关的状态,是打开还是关闭。不确定我该怎么做?我是否要传递某种默认值,其中 0 表示关闭,1 表示打开?

  2. 目前,开关正在从数组中适当映射,但打开或关闭仅适用于第一个开关。因此,如果我点击任何其他开关,出于某种原因第一个开关会切换。

最佳答案

对于第 1 点,您可以使用 e.target.checked 检查特定 CustomInput 的真/假状态;检查this 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>
    );
  }
}

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

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

  • 你必须在构造函数中实例化登录状态
  • customInputSwitched 函数应该传递特定按钮的参数,而不是硬编码的“button1”——所以我们添加疾病的索引号
  • 所有按钮的ID不能是同一个'exampleCustomSwitch',所以我们在ID上加上索引号就可以了
  • 映射为数组的最佳做法是同时包含一个索引,这有好处(如接下来的两点所示)

相关工作 JS 用于您的代码/stackblitz:

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>
    );
  }
}

关于reactjs - 如何获取 Reactstrap 的 CustomInput Switch 组件的状态,以及如何从数组映射开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58219124/

相关文章:

html - 内部 CSS 功能不起作用

javascript - 如何在React的render()中导航到react-router-1.0.3中的组件之外?

javascript - 组件中的 SetTimeout 在 react 中执行两次

reactjs - Ag-Grid - 如何在同一个网格中制作两个水平滚动条?

node.js - 找不到模块 '@babel/core'

html - Bootstrap 页脚在调整为移动设备大小时左右间隙

css - Twitter Bootstrap 模式 z-index 不工作

css - 对 custom.css.scss 进行微小更改后 Bootstrap 无法正常工作

jquery - 如何更新blueimp jquery文件上传以使用Bootstrap 4+

html - CSS 按钮边框由于某种原因无法正常工作