Skip to Content
ohooks

useControllableState

Manages state that can be either controlled externally or uncontrolled with an internal fallback.

Loading preview...

The useControllableState hook powers components that support both controlled (value + onChange) and uncontrolled (defaultValue) usage patterns, automatically warning in development if a component switches between modes.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useControllableState.json

Usage

Examples

Building a Dual-Mode Switch Component

Accept optional checked, defaultChecked, and onCheckedChange props and delegate state management to useControllableState.

Functional Updates

Like useState, the setter accepts an updater function that receives the current resolved value regardless of whether the state is controlled or uncontrolled.

API Reference

UseControllableStateOptions

Configuration options for resolving controlled and uncontrolled state.

Property
Specification
value?
T
Controlled state value; when undefined, the hook manages state internally
defaultValuerequired
union
Initial state value (or lazy initializer) used in uncontrolled mode
onChange?
function
Callback fired whenever the state value changes
shouldUpdate?
functiondefault:Object.is
Equality comparator to skip redundant updates