代码之家  ›  专栏  ›  技术社区  ›  Mo.

react引导组件开关不工作

  •  0
  • Mo.  · 技术社区  · 6 年前

    引导程序 switches 似乎不起作用。即使是文档版本也不起作用

    <Form>
      <Form.Check 
        type="switch"
        id="custom-switch"
        label="Check this switch"
      />
      <Form.Check 
        disabled
        type="switch"
        label="disabled switch"
        id="disabled-custom-switch"
      />
    </Form>
    

    Edit angry-kepler-5wqi3

    0 回复  |  直到 6 年前
        1
  •  5
  •   Skipper    6 年前

    简单的FormCheck对我有用:

    <FormCheck 
      id="switchEnabled"
      type="switch"
      checked={this.state.settings.enabled}
      onChange={this.toggleEnabled}
      label="Enable"
    />
    

    关键点是提供id。另一个重要的事情(加载初始值)是使用 选中的

        2
  •  1
  •   DJ'    6 年前

    如果你加上 custom 属性它将显示开关按钮,但我仍然无法切换开关。。。

    <Form>
      <Form.Check
        custom
        type="switch"
        id="custom-switch"
        label="Check this switch"
      />
    </Form>
    
        3
  •  0
  •   Rahul    6 年前

    我找到了一个方法。

    import React from "react";
    import ReactDOM from "react-dom";
    import { Container, Form, FormCheck, Button } from "react-bootstrap";
    
    import "./styles.css";
    import "bootstrap/dist/css/bootstrap.min.css";
    
    function App() {
      const [swt, setSwt] = React.useState(true);
      const [swt2, setSwt2] = React.useState(true);
    
      return (
        <Container className="App">
          Aprroch 1
          <FormCheck custom type="switch">
            <FormCheck.Input isInvalid checked={swt} />
            <FormCheck.Label onClick={() => setSwt(!swt)}>
              {`Value is ${swt}`}
            </FormCheck.Label>
          </FormCheck>
          Approch 2
          <Form.Check custom type="switch">
            <Form.Check.Input isInvalid checked={swt2} />
            <Form.Check.Label onClick={() => setSwt2(!swt2)}>
              {`Value is ${swt2}`}
            </Form.Check.Label>
          </Form.Check>
        </Container>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    codeSandbox

        4
  •  0
  •   Simon    6 年前

    我有一个类似的问题,并添加自定义建议在另一个答案显示开关正确,但同样的切换现在可以工作。

    我注意到我指向的是react引导的一个旧版本,所以将此更改为指向当前版本,当时的版本是v1.0.0-beta.16,这允许切换正常工作,并且可以删除自定义。

    React Bootstrap

        5
  •  0
  •   Mix Master Mike    6 年前

    不幸的是,关于交换机的文档并不是最好的。不过,以下内容应该有助于设置开关供您使用。

    const [isSwitchOn, setIsSwitchOn] = useState(false);
    
    const onSwitchAction = () => {
      setIsSwitchOn(!isSwitchOn);
    };
    
    ...
    <Form>
      <Form.Switch
        onChange={onSwitchAction}
        id="custom-switch"
        label="anything you want to put here"
        checked={isSwitchOn}
        disabled // apply if you want the switch disabled
      />
    </Form>
    ...