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
You can use a Boolean value in the
checkedattribute: