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

懒汉堆栈

用于懒惰水平布局的 SwiftUI LazyHStack 组件。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.15

Expo UI 的 LazyHStack 与官方 SwiftUI LazyHStack API 一致,并且将其子元素水平排列。

🌐 Expo UI LazyHStack matches the official SwiftUI LazyHStack API and arranges its children horizontally.

安装

🌐 Installation

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun 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 lazy horizontal stack

把 LazyHStack 放到带有 axes="horizontal" 的 ScrollView 里。每个 LazyHStack 的子元素都是一个项目。使用 spacing 和 alignment 属性来调整项目的间距和对齐。

🌐 Put LazyHStack inside a ScrollView with axes="horizontal". Each child of LazyHStack is one item. Use the spacing and alignment props to space and align the items.

BasicLazyHStackExample.tsx
import { Host, ScrollView, LazyHStack, Text, } from '@expo/ui/swift-ui'; export default function BasicLazyHStackExample() { return ( <Host style={{ flex: 1 }}> <ScrollView axes="horizontal"> <LazyHStack spacing={12}> <Text>Photos</Text> <Text>Music</Text> <Text>Videos</Text> </LazyHStack> </ScrollView> </Host> ); }

大型列表

🌐 Large lists

使用 LazyHStack.ForEach 来渲染数组中的项目。把项目作为 data 传入,每个项目用 keyExtractor 提供一个唯一的 key,并传入一个 children 函数来返回项目的视图。LazyHStack.ForEach 只渲染靠近可见区域的项目,并在滚动时重用它们。

🌐 Use LazyHStack.ForEach to render items from an array. Pass the items as data, a unique key for each item with keyExtractor, and a children function that returns the view for an item. LazyHStack.ForEach renders only the items near the visible area and reuses them while you scroll.

LargeLazyHStackExample.tsx
import { Host, ScrollView, LazyHStack, Text, } from '@expo/ui/swift-ui'; const items = Array.from( { length: 10000 }, (_, i) => `Item ${i + 1}` ); function renderItem({ item }: { item: string }) { return <Text>{item}</Text>; } export default function LargeLazyHStackExample() { return ( <Host style={{ flex: 1 }}> <ScrollView axes="horizontal"> <LazyHStack spacing={12}> <LazyHStack.ForEach data={items} keyExtractor={item => item} estimatedItemSize={80}> {renderItem} </LazyHStack.ForEach> </LazyHStack> </ScrollView> </Host> ); }

在展示物品时遵守这些规则:

🌐 Follow these rules when you render items:

  • 在组件外定义 children 函数,或者把它包在 useCallback 里。否则,每次组件渲染时,可见区域附近的项都会重新渲染。
  • 一个重用的视图会保留它之前显示的项目的本地状态(useState)。当项目改变时重置状态,或者把状态保存在视图外部。
  • 一个还没准备好的项目会显示一个空占位符。将 estimatedItemSize 设置为接近你项目宽度的值,单位是点。
  • 使用 overscanCount 来在可见项目的前后准备更多项目。
  • 要更新项目,传入一个新的 data 数组。不要在原地修改数组。
  • 把 recycling={false} 设置成提前渲染每一项。

应用接口

🌐 API

import { LazyHStack } from '@expo/ui/swift-ui';

No API data file found, sorry!