开关
一个在开启和关闭状态之间切换的切换控件。
Android
iOS
Web
Included in Expo Go
For the complete documentation index, see llms.txt. Use this file to discover all available pages.
受控切换。将 value 与 onValueChange 配对使用,以便从 React 管理状态。
安装
Terminal
- npx expo install @expo/uiIf 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 style={{ flex: 1 }}> <Switch label="启用通知" value={notifications} onValueChange={setNotifications} /> </Host> ); }
API
import { Switch } from '@expo/ui';
Component
Type: React.Element<SwitchProps>
A toggle control that switches between on and off states.
Props for the Switch component, a toggle control.
Optional • Type:
booleanWhether the switch is disabled. Disabled switches do not respond to user interaction.
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.
Optional • Type:
stringIdentifier used to locate the component in end-to-end tests.