createControllableValue
Bridge controlled and uncontrolled component state with a single reactive accessor and setter pair.
createControllableValue bridges controlled and uncontrolled component state. It allows custom form controls to seamlessly support both caller-managed values (props.value) and self-managed fallback values (props.defaultValue).
Import#
Signature#
Usage Example#
src/CustomToggle.tsx
Behavior#
- Controlled Mode: When
options.value()returns a defined value, the returned accessor follows that value. CallingsetValuewill not mutate local state, but callers can intercept state in their event handlers. - Uncontrolled Mode: When
options.value()returnsundefined, the hook manages state internally starting withoptions.defaultValue(). - Mode Switching: Mode changes between controlled and uncontrolled are tracked reactively.