useToggle
Custom hook that manages a boolean toggle state in React components.
Custom hook that manages a boolean toggle state in React components.
Usage
import { useToggle } from "@tilt-legal/cubitt-components/utilities/hooks";
const [isToggled, toggle, setToggle] = useToggle(); // Initial value is false
// OR
const [isToggled, toggle, setToggle] = useToggle(true); // Initial value is true
// Use isToggled in your component, toggle to switch the state, setToggle to set the state explicitly.API
Signature
declare function useToggle(defaultValue?: boolean): [boolean, () => void, Dispatch<SetStateAction<boolean>>];Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
[defaultValue] | boolean | The initial value for the toggle state. | - |
Return Value
| Value | Type | Description |
|---|---|---|
return | [boolean, () => void, Dispatch<SetStateAction<boolean>>] | A tuple containing the current state, a function to toggle the state, and a function to set the state explicitly. |