raddix

useBoolean

Manages a boolean value with useful utility functions.

Installation

Install the custom hook from your command line.

npm i @raddix/use-boolean

Usage

In this example, we will open a menu when the cursor hovers over the button and it will close when the cursor leaves the menu container. If you click on the button, it will open/close.

import { useBoolean } from '@raddix/use-boolean';
import { Menu } from './menu'
export default function App() {
const [open, setOpen] = useBoolean();
return (
<div onMouseLeave={setOpen.off}>
<button onMouseEnter={setOpen.on} onClick={setOpen.toggle}>
Menu
</button>
<Menu />
</div>
)
}

API

Parameters

NameDescriptionRequiredDefault Value
initialValueThe initial value of the boolean state.Nofalse

Return

The useBoolean hook returns an array with the following elements:

IndexDescription
[0]The current value of the state.
[1].onUna función para establecer el valor booleano en true.
[1].offUna función para establecer el valor booleano en false.
[1].toggleUna función para negar el estado booleano.