Library: React v.18.0
CSS: React-bootstrap v.5.0 framework
Component: Toggle switch
Official toggle switch documentation: https://react-bootstrap.github.io/docs/forms/checks-radios/#switches
Requirement: To have one switch with dynamic checked and unchecked attribute in the <Form.Check
using let
or useState
hook.
Try 1: Used if...else
expression in return
statement to toggle the checked
and unchecked
attribute.
Try 2: Took the <Form.Check
to a different function and did an if else with 2 separate <Form.check
one with checked
attribute another with unchecked
attribute.
Coder view: https://stackblitz.com/edit/react-ug9ffy?file=src%2FApp.js
User view: https://react-ug9ffy.stackblitz.io
2
Answers
You can use a boolean value in
checked
attribute:So I think you want a variable amount of checkboxes. To do this and to control the states of it, you should make an external component for a checkbox.
Something like this: