This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 56).
警报
一个用于展示原生 iOS 警报对话框的 SwiftUI Alert 组件。
For the complete documentation index, see llms.txt. Use this file to discover all available pages.
Expo UI Alert 与官方 SwiftUI alert API 一致,并会显示一个原生 iOS 警告对话框,其中包含标题、操作按钮以及可选消息。

Alert 是居中的模态对话框,对应于 ConfirmationDialog,后者会从屏幕底部以操作表的形式呈现。二者共享相同的 trigger/actions/message 插槽模型,因此调用方只需更改组件名称,就可以在它们之间切换。
安装
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
基础警告
使用 Alert.Trigger 来定义可见元素,并使用 Alert.Actions 来提供对话框按钮。
import { useState } from 'react'; import { Host, Alert, Button } from '@expo/ui/swift-ui'; export default function BasicAlertExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host matchContents> <Alert title="Saved" isPresented={isPresented} onIsPresentedChange={setIsPresented}> <Alert.Trigger> <Button label="Show alert" onPress={() => setIsPresented(true)} /> </Alert.Trigger> <Alert.Actions> <Button label="OK" onPress={() => setIsPresented(false)} /> </Alert.Actions> </Alert> </Host> ); }
取消与确认
将 role="cancel" 与确认按钮结合使用,可以构建一个标准的是/否警告框。
import { useState } from 'react'; import { Host, Alert, Button, Text } from '@expo/ui/swift-ui'; export default function CancelConfirmAlertExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host matchContents> <Alert title="Sign out?" isPresented={isPresented} onIsPresentedChange={setIsPresented}> <Alert.Trigger> <Button label="Sign out" onPress={() => setIsPresented(true)} /> </Alert.Trigger> <Alert.Actions> <Button label="Sign Out" onPress={() => console.log('Signed out')} /> <Button label="Cancel" role="cancel" /> </Alert.Actions> <Alert.Message> <Text>You will need to sign in again to access your account.</Text> </Alert.Message> </Alert> </Host> ); }
危险操作
在 Alert.Actions 内的 Button 上使用 role="destructive",可将其样式设为危险操作。
import { useState } from 'react'; import { Host, Alert, Button, Text } from '@expo/ui/swift-ui'; export default function DestructiveAlertExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host matchContents> <Alert title="Delete account?" isPresented={isPresented} onIsPresentedChange={setIsPresented}> <Alert.Trigger> <Button label="Delete account" role="destructive" onPress={() => setIsPresented(true)} /> </Alert.Trigger> <Alert.Actions> <Button label="Delete" role="destructive" onPress={() => { console.log('Deleted'); setIsPresented(false); }} /> <Button label="Cancel" role="cancel" /> </Alert.Actions> <Alert.Message> <Text>This permanently deletes your account and all data. This cannot be undone.</Text> </Alert.Message> </Alert> </Host> ); }
API
import { Alert } from '@expo/ui/swift-ui';
Component
Type: React.Element<AlertProps>
Alert presents a SwiftUI alert with a title, optional message, and action buttons.
See: Official SwiftUI documentation.