Hook
useControllableState
A small helper for components that support both controlled and uncontrolled state.
Hook
02
Import
import { useControllableState } from "@swirski/ui";Preview
useControllableState
PreviewInternal helper
Use this to keep component APIs consistent across Swirski primitives.
Usage
const [value, setValue] = useControllableState({
value: props.value,
defaultValue: props.defaultValue ?? "",
onChange: props.onValueChange,
});Props
| Prop | Type | Default | Notes |
|---|---|---|---|
value | T | - | Controlled value. |
defaultValueRequired | T | - | Uncontrolled initial value. |
onChange | (value: T) => void | - | Called when the setter resolves a value. |
Returns
| Value | Type | Notes |
|---|---|---|
value | T | Current controlled or uncontrolled value. |
setValue | Dispatch<SetStateAction<T>> | Updates internal state or calls onChange with the resolved value. |