react-use-boolean
v1.0.1
Published
React hook for changing booleans
Downloads
63
Readme
useBoolean
This is a simple hook to manage boolean state, useful for doing stuff like managing checkbox state, showing modals etc.
Installation
npm i react-use-boolean
or
yarn add react-use-boolean
Usage
import { useBoolean } from "react-use-boolean";
const YourComponent = () => {
const [value, actions] = useBoolean();
return (
<div>
{value && <p>Toggle me</p>}
<button onClick={actions.toggle}>Toggle</button>
<button onClick={actions.on}>Show</button>
<button onClick={actions.off}>Hide</button>
</div>
);
};
Reference
const [value, { on, off, toggle, set }] = useBoolean();
on(): void
- sets state totrue
;off(): void
- sets state tofalse
;toggle(): void
- inverts state;set(value: boolean): void
- sets state tovalue
specified;