useToggle
A custom hook that makes it easy to use a boolean state as a Toggle.
Code
Interface
typescript
function useToggle(defaultValue?: boolean): [boolean, () => void]
Options
| Name | Type | Default | Description |
|---|---|---|---|
defaultValue | boolean | false | The initial boolean value of the toggle |
Returns
| Name | Type | Description |
|---|---|---|
[0] | boolean | The current toggle state |
[1] | () => void | Function to switch the toggle state |
Usage
typescript
import { useToggle } from '@modern-kit/react';
const Example = () => {
const [bool, toggle] = useToggle(false);
return (
<div>
{bool && <p>render</p>}
<button onClick={toggle}>Toggle Button</button>
</div>
);
}