useBoolean
React hook for managing boolean state with convenient helper methods for setting true, false, and toggling. Perfect for React applications requiring clean boolean state management with Next.js integration and TypeScript support.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-boolean.json
npx shadcn@latest add https://www.shadcn.io/registry/use-boolean.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-boolean.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-boolean.json
Features
- Boolean state management with useState under the hood and validation
- Convenient helpers including setTrue, setFalse, and toggle methods
- Memoized callbacks using useCallback for optimal performance
- Type safety with comprehensive TypeScript definitions and error handling
- Default value support with boolean validation and error throwing
- Memory efficient with proper callback memoization to prevent unnecessary re-renders
Examples
Advanced Interface Controls
Demonstrates multiple boolean states working together in a realistic interface with dark mode, modals, and loading states.
Use Cases
This free open source React hook works well for:
- Modal dialogs - Show/hide modal states with convenient open/close methods
- Toggle switches - Dark mode, settings, and feature toggles built with Next.js
- Loading states - Start/stop loading indicators using TypeScript patterns
- Visibility controls - Show/hide components with clean boolean management
- Form validation - Track validation states and error conditions
- UI interactions - Hover states, dropdown menus, and accordion panels
API Reference
useBoolean
Parameter | Type | Default | Description |
---|---|---|---|
defaultValue | boolean | false | Initial boolean value. Must be a valid boolean or throws error |
Return Object
Property | Type | Description |
---|---|---|
value | boolean | Current boolean state value |
setValue | React.Dispatch<React.SetStateAction<boolean>> | Direct state setter (same as useState) |
setTrue | () => void | Memoized function to set state to true |
setFalse | () => void | Memoized function to set state to false |
toggle | () => void | Memoized function to toggle the current state |
Usage Patterns
Basic Usage
const { value, setTrue, setFalse, toggle } = useBoolean(false);
// Set to true
setTrue();
// Set to false
setFalse();
// Toggle current value
toggle();
With Initial Value
const { value, toggle } = useBoolean(true); // starts as true
Modal Management
const { value: isOpen, setTrue: openModal, setFalse: closeModal } = useBoolean();
return (
<>
<button onClick={openModal}>Open Modal</button>
{isOpen && <Modal onClose={closeModal} />}
</>
);
Implementation Notes
- Hook validates that defaultValue is a boolean and throws descriptive error if not
- All helper functions (setTrue, setFalse, toggle) are memoized with useCallback
- Callback memoization prevents unnecessary re-renders in child components
- Compatible with React 16.8+ and follows hooks rules and conventions
- Works seamlessly with TypeScript for full type safety and IntelliSense
- Helper methods have stable references across re-renders for performance
- Error handling provides clear feedback for invalid default values
React Hooks Shadcn Registry
Professional React hooks registry with 40+ copy-paste custom hooks for JavaScript, TypeScript, and Next.js. Install individual hooks directly into your project via shadcn/ui CLI. Full source code ownership, complete customization, and production-ready implementations.
useClickAnyWhere
Document-wide click event detection for React applications. Perfect for Next.js projects requiring global click tracking with TypeScript support and seamless integration.