useToggle
Toogle a boolean value
The useToggle
hook is helpful when you want to managing a boolean state and provide a function to toggle it.
It is useful when you want to take an opposite action from the current state like hide/show a modal, open/close a navbar etc.
useToggle
Current Value: true
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
Properties
- The
useToggle
correctly establishes its initial value, casting it to a boolean if it’s not one (using theBoolean
constructor) - Based on the argument passed to the toggle function returned from
useToggle
it will:- Toggle the old state if any non
boolean
value is passed. - Sets the state directly when a
true
orfalse
is passed.
- Toggle the old state if any non
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
initialValue | unknown | The initial value to track and toggle. | N/A |
Return Value
It returns a tuple with [state, toggleFn]
format similar to React’s useState
Parameter | Type | Description | Default |
---|---|---|---|
state | boolean | The current boolean state. | N/A |
toggleFn | (newValue?: unknown) => void | The update method to toggle the state. | N/A |