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

开关

一个可以在开和关状态之间切换的切换控件。

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

受控切换。将 valueonValueChange 配对,以便从 React 管理状态。

🌐 A controlled toggle. Pair value with onValueChange to manage state from React.

安装

🌐 Installation

Terminal
npx expo install @expo/ui

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

用法

🌐 Usage

基本开关

🌐 Basic switch

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> ); }

带标签

🌐 With label

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

🌐 When label is provided, the switch is rendered alongside its text in a labeled row.

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

应用接口

🌐 API

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

Component

Switch

Type: React.Element<SwitchProps>

A toggle control that switches between on and off states.

Props for the Switch component, a toggle control.

SwitchProps

disabled

Optional • Type: boolean

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

label

Optional • Type: string

Text label displayed alongside the switch.

modifiers

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

Type: (value: boolean) => void

Called when the user toggles the switch.

testID

Optional • Type: string

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

value

Type: boolean

Whether the switch is on.