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
Interface
typescript
function useToggleState<T>(value1: T, value2: T): [T, () => void]
Parameters
| Name | Type | Description |
|---|---|---|
value1 | T | The first toggle value |
value2 | T | The second toggle value |
Returns
| Name | Type | Description |
|---|---|---|
[0] | T | The currently selected value |
[1] | () => void | Function 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