useControllableState
State that works both controlled and uncontrolled.
State that works both controlled (value and onValueChange) and uncontrolled (defaultValue), for switches, segmented controls and chips.
Free (MIT)Version 1.0.0Released 23 Oct 2026iOS and Android
When to use it
- Share one value API between controlled and uncontrolled component use.
When not to use it
- Use local useState when the component never accepts a controlled value.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add use-controllable-state- Adds
components/swipeui/hooks/use-controllable-state.ts.
The hook behind Switch, SegmentedControl and Chip. Use it in your own components to accept either value with onValueChange, or just a defaultValue.
Usage
import { useControllableState } from '@/components/swipeui/hooks/use-controllable-state';
function Stepper({ value, defaultValue = 0, onValueChange }: StepperProps) {
const [count, setCount] = useControllableState({ value, defaultValue, onChange: onValueChange });
return <Button onPress={() => setCount(count + 1)}>{`${count}`}</Button>;
}When value is defined, the component follows it and only reports changes. Otherwise it keeps its own state. Setting the current value again doesn't call onChange.
Options
| Prop | Type | Default | Description |
|---|---|---|---|
value | | — | The controlled value. When it is defined, the component follows it. |
defaultValue | | — | The starting value when uncontrolled. |
onChange | | — | Called with every new value, controlled or not. |
Accessibility
- State ownership does not add roles or announce changes; expose them in the control.
Motion
- Static on its own; any movement comes from the parent that animates it.
- No custom movement is configured by this item.
Performance
- Calls onChange only when the next value differs.
Notes for AI
- Pass undefined for value to use the internal defaultValue.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.