shadcn.io is an independent project built on shadcn/ui
useBoolean keeps one boolean, plus setTrue, setFalse, and toggle.
Use it for a dialog, a disclosure, or a flag. When the two states are not true and false, use useToggle. In React those three functions stay the same between renders, so a button can take them as its click handler.
Installation
Anatomy

Usage
import { useBoolean } from "@/hooks/use-boolean";const { value, setTrue, setFalse, toggle } = useBoolean();Pass true to start on. setValue is there when you already have the next boolean, the same as useState.
FAQ
When should I use useBoolean instead of useState?
Use it when you want setTrue, setFalse, and toggle without writing them. Those three are memoized, so a child button can take them without a new function on every render. useState is enough when you only set the boolean in one place and don't pass the setter down.
How do I open and close a dialog with useBoolean?
Alias the fields: const { value: open, setTrue: show, setFalse: hide } = useBoolean(). Pass show and hide to the buttons. The dialog reads open. You don't need a fresh arrow function in each onClick.
Can I connect useBoolean to a Switch?
Yes. toggle ignores extra arguments, so <Switch checked={value} onCheckedChange={toggle} /> works. The switch passes the next boolean and useBoolean flips whatever it currently is.
Is useBoolean safe to render on the server?
Yes. It only uses useState and useCallback. It never reads window or document, so the first render on the server and the client is the default you passed. A default that is not true or false throws on both.
API Reference
Parameters
defaultValue?boolean
The value it starts with. Must be a boolean: anything else throws.
- Default
false
Returns
setFalse() => void
Sets the value to false. Stable across renders.
- Type
() => void
setTrue() => void
Sets the value to true. Stable across renders.
- Type
() => void
setValueReact.Dispatch<React.SetStateAction<boolean>>
Sets the value directly, or from the current one: useState's setter.
- Type
React.Dispatch<React.SetStateAction<boolean>>
toggle() => void
Flips the value. Stable across renders.
- Type
() => void
valueboolean
The current value.











