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

ParameterTypeDescriptionDefault
[defaultValue]booleanThe initial value for the toggle state.-

Return Value

ValueTypeDescription
return[boolean, () =&gt; void, Dispatch&lt;SetStateAction&lt;boolean&gt;&gt;]A tuple containing the current state, a function to toggle the state, and a function to set the state explicitly.

On this page