Typed HooksGetting StarteduseBoolean

useBoolean

Custom hook that handles boolean state with useful utility functions.

useBoolean is a simple but powerful custom React hook that provides convenient utilities for managing boolean state values. It encapsulates common boolean operations like toggling, setting to true/false, and resetting to the initial value.

This hook is particularly useful for managing UI states such as modals, accordions, toggles, and other interactive elements that need boolean control with clean, semantic function names.

Current value
false
Note: "Toggle" and "Set Opposite" perform the same action but use different hook methods.

Installation

pnpm dlx shadcn@latest add https://shaddy-docs.vercel.app/r/use-boolean

API

useBoolean(initialValue): [boolean, Controls]

NameTypeRequiredDescription
initialValuebooleanYesThe initial value for the boolean state.

Returns [value, controls]:

  • value (boolean): The current boolean state value.
  • controls (object):
    • setValue(value: boolean): Set the state directly.
    • toggle(): Toggle the state.
    • setTrue(): Set state to true.
    • setFalse(): Set state to false.
    • reset(): Reset to initial value.
    • getValue(): Get the current value.