useToggle
React state hook that tracks value of a boolean
Usage
Live Editor
function Demo() { const [on, toggle] = useToggle(true); return ( <div> <div>{on ? "ON" : "OFF"}</div> <button onClick={toggle}>Toggle</button> <button onClick={() => toggle(true)}>set ON</button> <button onClick={() => toggle(false)}>set OFF</button> </div> ); };
Result
Loading...
API
useToggle
Returns
[boolean, (nextValue?: any) => void]
: A tuple with the following elements:
- The current value of the bool state.
- A function to update the value of the bool state.
Arguments
Argument | Description | Type | DefaultValue |
---|---|---|---|
initialValue | initialValue | boolean (Required) | - |