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
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 internallyFull Signature
defaultValuerequiredunion
Initial state value (or lazy initializer) used in uncontrolled mode
Full Signature
onChange?function
Callback fired whenever the state value changes
Full Signature
Parameters
(valueTshouldUpdate?functiondefault:Object.is
Equality comparator to skip redundant updates
Full Signature
Parameters
(prevT, next: T