tanstack-virtual
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Virtual
TanStack Virtual
Overview
概述
TanStack Virtual is a headless UI utility for virtualizing large lists, grids, and tables. It renders only the visible items in the viewport, dramatically reducing DOM nodes and improving performance for datasets with thousands of rows. Framework adapters are available for React, Vue, Solid, Svelte, Lit, and Angular.
When to use: Rendering thousands of rows or columns, building virtualized tables, implementing infinite scroll, displaying large datasets where DOM node count impacts performance.
When NOT to use: Small lists under ~100 items (no performance benefit), server-rendered static content, layouts where all items must be in the DOM for SEO or accessibility, simple pagination (render one page at a time instead).
TanStack Virtual 是一款无UI的工具库,用于对大型列表、网格和表格进行虚拟化处理。它仅渲染视口中可见的项目,大幅减少DOM节点数量,提升包含数千行数据的数据集的性能。目前提供了适配React、Vue、Solid、Svelte、Lit和Angular等框架的版本。
适用场景: 渲染数千行或列数据、构建虚拟化表格、实现无限滚动、展示因DOM节点数量影响性能的大型数据集。
不适用场景: 少于约100条数据的小型列表(无性能提升)、服务端渲染的静态内容、所有项目必须在DOM中以满足SEO或无障碍需求的布局、简单分页场景(应改为一次渲染一页数据)。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Vertical list | | Wrap items in absolute-positioned container |
| Horizontal list | | Use |
| Grid layout | Row virtualizer + column virtualizer | Two virtualizer instances sharing one scroll element |
| Dynamic sizing | | Set |
| Window scroller | | No |
| Scroll to item | | Supports |
| Scroll to offset | | Supports |
| Custom keys | | Stable keys improve measurement cache |
| Overscan | | Renders extra items outside viewport (default: 1) |
| Gap between items | | Space between virtualized items in pixels |
| Multi-lane layout | | Masonry-style column layouts |
| Padding | | Space before first and after last item |
| RTL support | | Right-to-left horizontal scrolling |
| Disable virtualizer | | Renders nothing, resets internal state |
| Force remeasure | | Call after external layout changes |
| 模式 | API | 核心要点 |
|---|---|---|
| 垂直列表 | | 将项目包裹在绝对定位的容器中 |
| 水平列表 | | 使用 |
| 网格布局 | 行虚拟化器 + 列虚拟化器 | 两个虚拟化器实例共享同一个滚动元素 |
| 动态尺寸 | | 为每个元素设置 |
| 窗口滚动器 | | 无需设置 |
| 滚动到指定项 | | 支持 |
| 滚动到指定偏移 | | 支持 |
| 自定义键值 | | 稳定的键值可优化测量缓存 |
| 过度渲染 | | 渲染视口外的额外项目(默认值:1) |
| 项目间距 | | 虚拟化项目之间的间距(单位:像素) |
| 多列布局 | | 瀑布流样式的列布局 |
| 内边距 | | 第一个项目前和最后一个项目后的间距 |
| RTL支持 | | 从右到左的水平滚动 |
| 禁用虚拟化器 | | 不渲染任何内容,重置内部状态 |
| 强制重新测量 | | 在外部布局变化后调用 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Missing | Parent element must have |
Using | Use |
Forgetting | The total-size container must be |
Not setting | Dynamic measurement requires |
Setting | Overestimate to prevent scroll jumping; use largest expected size |
Recreating | Use a ref callback: |
Not using | Inner container height/width must equal |
Absolute positioning without | Use |
Using | |
| 错误内容 | 正确做法 |
|---|---|
滚动容器缺少 | 父元素必须设置 |
使用 | 使用 |
忘记为内部容器设置 | 总尺寸容器必须设置 |
使用 | 动态测量要求为每个元素设置 |
为动态项目设置的 | 高估尺寸以防止滚动跳变;使用预期的最大尺寸 |
每次渲染时重新创建 | 使用ref回调: |
未使用 | 内部容器的高度/宽度必须等于 |
使用绝对定位但未设置 | 使用 |
在未设置固定尺寸的情况下使用 | |
Delegation
任务委托
If theskill is available, delegate data table virtualization to it. TanStack Table has built-in virtualization integration. Otherwise, recommend:tanstack-tablenpx skills add oakoss/agent-skills --skill tanstack-tableIf theskill is available, delegate data fetching and infinite query patterns to it. Otherwise, recommend:tanstack-querynpx skills add oakoss/agent-skills --skill tanstack-query
如果技能可用,将数据表格虚拟化任务委托给它。TanStack Table 内置了虚拟化集成。 否则,推荐执行:tanstack-tablenpx skills add oakoss/agent-skills --skill tanstack-table如果技能可用,将数据获取和无限查询模式的任务委托给它。 否则,推荐执行:tanstack-querynpx skills add oakoss/agent-skills --skill tanstack-query
References
参考资料
- List virtualization with useVirtualizer
- Grid virtualization with row and column virtualizers
- Dynamic sizing with measureElement
- Infinite scroll with TanStack Query integration
- 使用useVirtualizer实现列表虚拟化
- 使用行和列虚拟化器实现网格虚拟化
- 使用measureElement实现动态尺寸
- 与TanStack Query集成实现无限滚动