Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Switch

在开启和关闭状态之间切换的切换控件

Android
iOS
Web
Included in Expo Go
Recommended version:
~58.0.1

一个受控切换开关。将 value 与 onValueChange 配对使用,从 React 管理状态。

两个展示开启和关闭状态的 Material 3 开关两个展示开启和关闭状态的 Material 3 开关

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基本开关

SwitchExample.tsx
import { useState } from 'react'; import { Host, Switch } from '@expo/ui'; export default function SwitchExample() { const [enabled, setEnabled] = useState(false); return ( <Host matchContents> <Switch value={enabled} onValueChange={setEnabled} /> </Host> ); }

带标签

提供 label 时,开关会与其文本一起显示在带标签的行中。

LabeledSwitchExample.tsx
import { useState } from 'react'; import { Host, Switch } from '@expo/ui'; export default function LabeledSwitchExample() { const [notifications, setNotifications] = useState(true); return ( <Host matchContents={{ vertical: true }} style={{ width: '100%' }}> <Switch label="Enable notifications" value={notifications} onValueChange={setNotifications} /> </Host> ); }

API

import { Switch } from '@expo/ui';

Component

Switch

Android
iOS
Web

Type: React.Element<SwitchProps>

A toggle control that switches between on and off states.

Props for the Switch component, a toggle control.

SwitchProps

disabled

Android
iOS
Web
Optional • Type: boolean

Whether the switch is disabled. Disabled switches do not respond to user interaction.

label

Android
iOS
Web
Optional • Type: string

Text label displayed alongside the switch.

modifiers

Android
iOS
Web
Optional • Type: ModifierConfig[]

Platform-specific modifier escape hatch. Pass an array of modifier configs from @expo/ui/swift-ui/modifiers or @expo/ui/jetpack-compose/modifiers.

onValueChange

Android
iOS
Web
Type: (value: boolean) => void

Called when the user toggles the switch.

testID

Android
iOS
Web
Optional • Type: string

Identifier used to locate the component in end-to-end tests.

value

Android
iOS
Web
Type: boolean

Whether the switch is on.