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:
~56.0.16

@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('Pressed')} /> </Column> </Host> ); }

可用组件

CategoryComponents
ContainerHost,每个通用子树所必需的根节点。
LayoutColumnRowSpacerScrollView
DisplayTextIcon
ControlsButtonSwitchCheckboxSliderTextInputPicker
Disclosure and presentationBottomSheetCollapsible
Collections and formsList(配合 ListItem)、FieldGroup

何时使用它而不是 jetpack-compose/swift-ui

  • 当你想要一棵在 Android、iOS 和 web 上都能不经修改运行的组件树时,请使用 universal 组件。由于这些组件在底层委托给 Jetpack Compose/SwiftUI,Android 和 iOS 上的平台原生外观与交互体验会得到保留。
  • 当你需要平台特定的控件、修饰符或行为,而 universal API 没有提供时,请直接使用 @expo/ui/jetpack-compose@expo/ui/swift-ui