You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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}>Off</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}>Off</button></>)}