A React hook for controlling a boolean value with toggle, on, and off callbacks. This
is extremely useful for adding controlled/uncontrolled component behavior to components
like <Checkbox/>, <Toggle/>, <Modal/>, etc.
Quick Start
importuseSwitchfrom'@react-hook/switch'// Basic usageconstComponent=(props)=>{const[value,toggle]=useSwitch(false/*default value*/)return(<><span>Value: {value}</span>{/* toggles the current value to its opposite*/}<buttononClick={toggle}>Toggle</button>{/* toggles the current value to true*/}<buttononClick={toggle.on}>On</button>{/* toggles the current value to false*/}<buttononClick={toggle.off}>On</button></>)}// Creating a toggle component with a controlled and uncontrolled// value patternconstToggle=({value: controlledValue, defaultValue, onChange})=>{const[value,toggle]=useSwitch(defaultValue,controlledValue,onChange)return(<><span>Value: {value}</span><buttononClick={toggle}>Toggle</button><buttononClick={toggle.on}>On</button><buttononClick={toggle.off}>On</button></>)}