This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
LazyHStack
一个用于惰性水平布局的 SwiftUI LazyHStack 组件。
Expo UI LazyHStack 与官方 SwiftUI LazyHStack API 一致,并沿水平方向排列其子项。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
基本的懒加载水平堆栈
应在 axes="horizontal" 的 ScrollView 内使用 LazyHStack。LazyHStack 的每个子项都是一个项目。使用 spacing 和 alignment 属性设置项目之间的间距和对齐方式。
大型列表
使用 LazyHStack.ForEach 从数组渲染项目。将项目作为 data 传入,使用 keyExtractor 为每个项目提供唯一键,并传入一个返回项目视图的 children 函数。LazyHStack.ForEach 只渲染可见区域附近的项目,并在滚动时复用这些项目。
渲染项目时请遵循以下规则:
- 在组件外定义
children函数,或使用useCallback包装它。否则,每次组件渲染时,可见区域附近的项目都会重新渲染。 - 复用的视图会保留之前显示项目的本地状态(
useState)。项目变化时重置状态,或将状态保存在视图外部。 - 尚未准备好的项目会显示空占位符。将
estimatedItemSize设置为接近项目宽度的值,单位为点。 - 使用
overscanCount在可见项目前后预先准备更多项目。 - 要更新项目,请传入新的
data数组。不要原地修改数组。 - 设置
recycling={false}可预先渲染每个项目。
API
import { LazyHStack } from '@expo/ui/swift-ui';
Components
Type: React.Element<LazyHStackForEachProps<T>>
A block of recycled items inside LazyHStack. Only a small pool of items around the visible
range is mounted. Items that are not ready yet show a placeholder of estimatedItemSize, or of
the size last measured for that item.
Mount it as a direct child of LazyHStack.
(info: {
index: number,
item: T
}) => ReactElementRenders an item. Wrap it in useCallback, or every item re-renders on each parent render.
Recycled views are reused for other items, so their local state (useState) carries over.
Reset it when the item changes, or keep the state outside the view.
number • Default: 64Placeholder size in points along the stack axis, until an item is measured. Must be positive.
Ignored when recycling is false.
(item: T, index: number) => stringReturns a stable, unique string key for each item.
number • Default: 10Extra items to prepare on each side of the visible items. Must be a non-negative integer.
Ignored when recycling is false.
Type: React.Element<LazyHStackProps>
stringThe vertical alignment of children within the stack.
Acceptable values are: 'center' | 'top' | 'bottom' | 'firstTextBaseline' | 'lastTextBaseline'
ReactNode