This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 54).
Switch
A Jetpack Compose Switch component for toggle controls.
Expo UI Switch matches the official Jetpack Compose Switch and Checkbox APIs and supports switch, checkbox, and button variants.
Installation
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
Usage
Toggle switch
Use the variant="switch" prop to display a standard toggle switch.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function ToggleSwitchExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Play music" variant="switch" color="#6200EE" /> </Host> ); }
Checkbox
Use the variant="checkbox" prop to display a checkbox control.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function CheckboxExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Accept terms" variant="checkbox" color="#6200EE" /> </Host> ); }
Button variant
Use the variant="button" prop to display a toggle button.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function ButtonVariantExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Notifications" variant="button" color="#6200EE" /> </Host> ); }
API
import { Switch } from '@expo/ui/jetpack-compose';
Components
Type: React.Element<SwitchProps>
stringLabel for the switch.
On Android, the label has an effect only when the
Switchis used inside aContextMenu.
(value: boolean) => voidCallback function that is called when the checked state changes.
Type: React.Element<ThumbContentProps>
Custom content to be displayed inside the switch thumb.