-
Notifications
You must be signed in to change notification settings - Fork 0
useBoolean
Easily manage a boolean state with dedicated setTrue, setFalse, and toggle methods. This hook prevents the need to write redundant callback functions like () => setX(true) all over your components, keeping your JSX clean and your callbacks stable.
A simple toggleable UI element.
import { useBoolean } from "react-hook-lab";
function Menu() {
const menu = useBoolean(false);
return (
<div>
<button onClick={menu.toggle}>
{menu.value ? "Close Menu" : "Open Menu"}
</button>
{menu.value && (
<ul>
<li>Profile</li>
<li>Settings</li>
</ul>
)}
</div>
);
}Using the dedicated setTrue and setFalse methods for explicit actions.
import { useBoolean } from "react-hook-lab";
function Modal() {
const { value: isOpen, setTrue: open, setFalse: close } = useBoolean(false);
return (
<div>
<button onClick={open}>Open Modal</button>
{isOpen && (
<div className="modal">
<p>Important Content!</p>
<button onClick={close}>Confirm & Close</button>
</div>
)}
</div>
);
}Passing stable callbacks down to highly optimized child components without triggering unnecessary re-renders.
import React, { memo } from "react";
import { useBoolean } from "react-hook-lab";
// An optimized component that checks referential equality of props
const OptimizedButton = memo(({ onClick, label }: { onClick: () => void, label: string }) => {
console.log(`Rendering ${label}`);
return <button onClick={onClick}>{label}</button>;
});
function Dashboard() {
const sidebar = useBoolean(false);
return (
<div>
{/* Since sidebar.setTrue and sidebar.setFalse are wrapped in useCallback,
OptimizedButton will NOT re-render when the parent renders. */}
<OptimizedButton onClick={sidebar.setTrue} label="Show Sidebar" />
<OptimizedButton onClick={sidebar.setFalse} label="Hide Sidebar" />
{sidebar.value && <aside>Sidebar Content</aside>}
</div>
);
}-
initialValue(Optional)- Type:
boolean - Default:
false - Description: The initial boolean state.
- Type:
Returns an object of type UseBooleanReturn containing:
-
value(boolean): The current boolean state. -
setValue(Dispatch<SetStateAction<boolean>>): Standard React state setter function. -
setTrue(() => void): A stable, memoized function that sets the state totrue. -
setFalse(() => void): A stable, memoized function that sets the state tofalse. -
toggle(() => void): A stable, memoized function that toggles the current state.
Under the hood, useBoolean is a robust wrapper around React's useState.
It holds a primitive boolean value in state, and provides stable, memoized functions (using useCallback with empty dependency arrays) for common boolean operations:
-
setTruealways invokessetValue(true). -
setFalsealways invokessetValue(false). -
toggleinvokessetValue(prev => !prev).
Because these modifier methods are guaranteed to be referentially stable across renders, you can safely pass them deeply into your component tree or down to React.memo components without triggering cascading re-renders.
Built with ❤️ by Saurav-TB-Pandey. | Report an Issue | NPM