Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-bootstrap component Switches not working

The bootstrap switches not seems to be working. Even the documentation version not workng

<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

like image 414
Mo. Avatar asked Sep 01 '19 17:09

Mo.


People also ask

How do I use the toggle button in react JS?

You have to update the state via useState hook and import it at the top level. let [changeText, setChangeText] = useState(true); The text in the bracket and assigned to some value is called array destructing.

What is variant in react bootstrap?

Variant is a prop for your React-Bootstrap components which wrap the various predefined styles for the different bootstrap components. You can't customise a React Bootstrap component using a variant(if you want to deviate from bootstrap's theme).


1 Answers

Unfortunately documentation for the switch isn't the greatest. Nevertheless, the following should help with setting up the switch for your use.

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>
...
like image 121
Mix Master Mike Avatar answered Oct 06 '22 20:10

Mix Master Mike