Skip to main content

useToggleState

A hook that provides the ability to toggle between two values.

It manages two values provided as defaults, and returns the selected value along with a function to toggle between the two.


Code

🔗 View source code


Interface

typescript
function useToggleState<T>(value1: T, value2: T): [T, () => void]

Parameters

NameTypeDescription
value1TThe first toggle value
value2TThe second toggle value

Returns

NameTypeDescription
[0]TThe currently selected value
[1]() => voidFunction to toggle between the two values

Usage

typescript
import { useToggleState } from '@modern-kit/react';

const Example = () => {
const [value, toggle] = useToggleState('ON', 'OFF');

return (
<div>
{value}
<button onClick={toggle}>Toggle Button</button>
</div>
);
}

Example

ON