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.

通用

用于使用 @expo/ui 在 Android、iOS 和 web 上构建共享 UI 的跨平台组件。

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

@expo/ui 中的通用组件是对平台原生 UI 工具包的一层单一 API 封装。在 Android 上,它们委托给 @expo/ui/jetpack-compose。在 iOS 上,它们委托给 @expo/ui/swift-ui。在 web 上,它们是使用 react-domreact-native-web 的 JS 实现,并且会根据具体组件进行选择,以适配相应的控件。

安装

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.

用法

通用组件仍然必须包装在 Host 中,但你需要从包根目录导入包括 Host 在内的所有内容。通用的 Host 会在 Android 和 iOS 上分发到平台原生的 host,因此无需直接使用 @expo/ui/swift-ui@expo/ui/jetpack-compose

UniversalExample.tsx
import { Host, Column, Button, Text } from '@expo/ui'; export default function Example() { return ( <Host style={{ flex: 1 }}> <Column spacing={12} alignment="center"> <Text>你好,世界!</Text> <Button label="按我" onPress={() => alert('已按下')} /> </Column> </Host> ); }

组件

何时使用这个与 swift-ui / jetpack-compose

  • 当你希望有一棵组件树能够在 Android、iOS 和 web 上不经修改地运行时,请选择 通用 组件。由于这些组件在底层委托给 Jetpack Compose/SwiftUI,Android 和 iOS 上的平台原生外观与交互体验得以保留。
  • 当你需要通用 API 未暴露的平台特定控件、修饰符或行为时,请直接选择 @expo/ui/swift-ui@expo/ui/jetpack-compose