createControllableValue
Bridge controlled and uncontrolled component state with a single reactive accessor and setter pair.
createControllableValue bridges controlled and uncontrolled state for a custom component. It follows the caller’s value while value() is defined, and otherwise keeps its own state, seeded from defaultValue().
Import#
Signature#
Usage Example#
src/CustomToggle.tsx
Behavior#
- Controlled: When
value()returns a defined value, the accessor returns it. The setter does not change internal state. It callsonChangewith the next value, and the caller updatesvalue. - Uncontrolled: When
value()returnsundefined, the hook holds the state internally, seeded fromdefaultValue(). The setter updates that state and callsonChange. defaultValue()is read once, when the hook runs.- The setter accepts a value or an updater function that receives the current value.
- The setter skips
onChangewhen the next value isObject.is-equal to the current one.