Overview
useToggle is a simple hook for managing boolean state values. It provides a convenient way to toggle between true and false, with support for explicitly setting the value.
Import
Signature
Parameters
The initial state value. Can be a boolean or a function that returns a boolean for lazy initialization.
Return Value
Returns a tuple with two elements:The current boolean state value.
Function to toggle or set the state:
- When called with no arguments: toggles the current value
- When called with a boolean: sets the value to that boolean
- When called with any other value: toggles the current value
Usage
Basic Toggle
Explicit Set Value
Lazy Initialization
Notes
- The toggle function is memoized with
useCallbackfor stable references - Passing a boolean value explicitly sets the state to that value
- Passing any non-boolean value (or no value) toggles the current state
- Supports lazy initialization through a function for the initial value