useToggle
Manage a boolean toggle state
Installation
Copy-paste the hook
Copy and paste the hook code in a .ts
file.
Usage
Details
Toggle Functionality
- The hook provides a simple and efficient way to manage a boolean toggle state, allowing you to easily switch between two states (on/off).
- It initializes the toggle state based on an optional
defaultValue
, which defaults tofalse
if not provided.
State Management
on
: A boolean state that indicates the current status of the toggle. It istrue
when the toggle is in the "on" position andfalse
when it is "off".setToggle
: A function that allows you to directly set the toggle state to a specific boolean value, providing flexibility in managing the toggle state programmatically.
Toggle Action
toggle
: A function that toggles the state ofon
betweentrue
andfalse
. This function can be called in response to user actions (e.g., button clicks) to change the toggle state seamlessly.
Return Value
- The hook returns an object containing:
on
: A boolean indicating the current state of the toggle (true
for "on",false
for "off").toggle
: A function to toggle the state between "on" and "off".setToggle
: A function to directly set the toggle state to a specific boolean value.