SwipeUI
DocsHooks & utilities

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

components/stepper.tsx
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

PropTypeDefaultDescription
valueT—

The controlled value. When it is defined, the component follows it.

defaultValueT—

The starting value when uncontrolled.

onChange((value: T) => void)—

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 onSupported
Expo SDK 57Yes
Expo SDK 58Yes
React Native CLI (with Expo modules)Yes
Expo GoYes
iOSYes
AndroidYes

This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.

Related items

On this page