Join our Discord Community

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.

Loading component...

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

Loading component...

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

ParameterTypeDefaultDescription
defaultValuebooleanfalseInitial boolean value. Must be a valid boolean or throws error

Return Object

PropertyTypeDescription
valuebooleanCurrent boolean state value
setValueReact.Dispatch<React.SetStateAction<boolean>>Direct state setter (same as useState)
setTrue() => voidMemoized function to set state to true
setFalse() => voidMemoized function to set state to false
toggle() => voidMemoized 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
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