useToggle
Custom hook that manages a boolean toggle state in React components.
Installation
npx sse-tool add use-toggle
npm install sse-hooks
Usage
import { useToggle } from "./{hooks file}";
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.
import { useToggle } from "sse-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
Parameters
| Name | Type | Description |
|---|---|---|
| defaultValue | boolean | The initial value for the toggle state. |
Return Value
Returns any.
A tuple containing the current state, a function to toggle the state, and a function to set the state explicitly.