react-virtualized、react-window 和 react-virtual 都是为了解决 React 应用中渲染大量列表数据时的性能问题而设计的库。它们通过“虚拟化”技术,只渲染用户可见区域的 DOM 节点,从而大幅降低内存占用和渲染时间。react-virtualized 是早期的标准方案,功能全面但较重;react-window 是其继任者,更轻量且专注;react-virtual 则提供了基于 Hook 的无头(headless)方案,灵活性最高。
在 React 开发中,当需要渲染成千上万条数据时,直接渲染会导致页面卡顿甚至崩溃。这三个库都通过“虚拟化”技术解决了这个问题 —— 它们只渲染用户可见区域内的元素,其余部分用占位符代替。虽然目标一致,但它们的设计理念和适用场景却有明显差异。
这三个库最大的区别在于它们如何暴露虚拟化逻辑给开发者。
react-virtualized 提供了一整套封装好的组件。
<List>、<Grid> 或 <Table> 组件。// react-virtualized: 使用封装组件
import { List } from 'react-virtualized';
<List
width={600}
height={400}
rowCount={1000}
rowHeight={50}
rowRenderer={({ index, style }) => (
<div style={style}>Row {index}</div>
)}
/>
react-window 提供了更轻量的组件,但要求你通过渲染属性(Render Props)提供内容。
// react-window: 使用渲染属性
import { FixedSizeList } from 'react-window';
<FixedSizeList
height={400}
width={600}
itemCount={1000}
itemSize={50}
>
{({ index, style }) => (
<div style={style}>Row {index}</div>
)}
</FixedSizeList>
react-virtual 不提供 UI 组件,只提供一个 Hook。
useVirtual 获取虚拟化数据。// react-virtual: 使用 Hook
import { useVirtual } from 'react-virtual';
import { useRef } from 'react';
function VirtualList() {
const parentRef = useRef();
const virtualizer = useVirtual({
size: 1000,
parentRef,
estimateSize: () => 50,
});
return (
<div ref={parentRef} style={{ height: 400, overflow: 'auto' }}>
<div style={{ height: virtualizer.getTotalSize() }}>
{virtualizer.virtualItems.map(item => (
<div
key={item.key}
style={{
position: 'absolute',
top: 0,
transform: `translateY(${item.start}px)`,
height: item.size,
}}
>
Row {item.index}
</div>
))}
</div>
</div>
);
}
在处理非标准布局时,这三个库的表现截然不同。
react-virtualized 强制使用绝对定位。
// react-virtualized: 样式由库控制
// rowRenderer 接收的 style 包含 position: absolute
rowRenderer={({ index, style }) => (
<div style={style}> {/* 难以修改定位方式 */} </div>
)}
react-window 同样使用绝对定位,但结构更清晰。
outerRef 和 innerRef 访问容器。// react-window: 内容区域可控
<FixedSizeList
innerElementType="ul" // 可以将内部容器改为 ul
>
{({ index, style }) => (
<li style={style}>Item {index}</li>
)}
</FixedSizeList>
react-virtual 让你手动应用样式。
start 和 size 数据,自己决定怎么写样式。// react-virtual: 手动应用样式
<div
style={{
position: 'absolute',
top: 0,
left: 0,
transform: `translate3d(0, ${item.start}px, 0)`,
}}
>
{item.index}
</div>
选择库时,维护状态是一个关键因素,它关系到未来的升级成本和安全性。
react-virtualized 已进入维护模式。
react-window。react-window 处于稳定维护期。
react-virtual 活跃开发中。
| 特性 | react-virtualized | react-window | react-virtual |
|---|---|---|---|
| API 类型 | 封装组件 (Class) | 轻量组件 (Render Props) | Hook (无头) |
| DOM 控制 | 低 (固定结构) | 中 (内容可控) | 高 (完全手动) |
| 布局方式 | 绝对定位 | 绝对定位 | 手动控制 (绝对/相对) |
| 维护状态 | 维护模式 (Legacy) | 稳定维护 | 活跃开发 |
| 学习曲线 | 中 (配置项多) | 低 (直观) | 高 (需理解原理) |
react-virtualized 就像一台重型机床 🏭—— 功能强大但笨重,适合旧项目维护。如果你的代码库已经大量使用它,迁移成本可能高于收益,可以暂时保留。但在新项目中,它不再是首选。
react-window 就像一把标准电钻 🔩—— 轻便、可靠、适合大多数任务。如果你只需要渲染标准的长列表或网格,不需要复杂的自定义布局,选它最稳妥。它的 API 设计合理,文档完善,能满足 90% 的需求。
react-virtual 就像一套精密的手工工具 🛠️—— 灵活、强大,但需要更多技巧。如果你需要实现非标准的滚动效果、动态高度计算或与现有布局系统深度集成,它的 Hook 设计能给你最大自由。适合对性能和控制力有极致要求的团队。
总结:对于大多数新项目,优先选择 react-window。如果需要极致灵活性或正在使用 TanStack 生态,选择 react-virtual。尽量避免在新项目中引入 react-virtualized。
如果你需要快速实现标准的长列表或网格,且希望拥有稳定的维护和较小的包体积,react-window 是最佳选择。它由 react-virtualized 的同一作者开发,专为现代 React 优化,API 简洁,适合大多数通用的虚拟化场景。
如果你的项目是旧代码库维护,且已经深度依赖 react-virtualized 的复杂组件(如 Table 或 Grid),继续使用它可以避免大规模重构。但在新项目中不建议选用,因为它主要基于 Class 组件,API 较重,且官方已建议转向更轻量的方案。
如果你需要极高的灵活性,希望完全控制 DOM 结构,或者正在使用 TanStack 生态系统,react-virtual 是理想方案。它基于 Hook 设计,无头(headless)特性让你可以自由组合样式和布局,非常适合需要定制化滚动行为或复杂布局的场景。
react-window is a component library that helps render large lists of data quickly and without the performance problems that often go along with rendering a lot of data. It's used in a lot of places, from React DevTools to the Replay browser.
If you like this project there are several ways to support it:
The following wonderful companies and individuals have sponsored react-window:
Begin by installing the library from NPM:
npm install react-window
TypeScript definitions are included within the published dist folder
Frequently asked questions can be found here.
Documentation for this project is available at react-window.vercel.app.
Renders data with many rows.
| Name | Description |
|---|---|
| rowComponent | React component responsible for rendering a row. This component will receive an ℹ️ The prop types for this component are exported as |
| rowCount | Number of items to be rendered in the list. |
| rowHeight | Row height; the following formats are supported:
ℹ️ Percentage heights can't be resolved during server rendering unless a ⚠️ Dynamic row heights are not as efficient as predetermined sizes. It's recommended to provide your own height values if they can be determined ahead of time. |
| rowProps | Additional props to be passed to the row-rendering component. List will automatically re-render rows when values in this object change. ⚠️ This object must not contain |
| Name | Description |
|---|---|
| className | CSS class name. |
| style | Optional CSS properties. The list of rows will fill the height defined by this style. |
| children | Additional content to be rendered within the list (above rows). This property can be used to render things like overlays or tooltips. |
| defaultHeight | Default height of list (in pixels) for initial render, before its actual height has been measured. ℹ️ Lists can't be measured during server rendering, so this value determines how many rows are server rendered.
It's recommended to set this to the expected height of the list (or to specify a pixel height via the |
| defaultScrollOffset | Initial scroll offset (in pixels) of the list. This value is only applied once, when the list first mounts with at least one row. ℹ️ Server-rendered HTML can't be pre-scrolled, so server rendering always starts at the first row; the default scroll offset is applied once the list hydrates. ⚠️ If the list can't be scrolled that far when the offset is applied (e.g. it is hidden with |
| listRef | Ref used to interact with this component's imperative API. This API has imperative methods for scrolling and a getter for the outermost DOM element. ℹ️ The |
| onResize | Callback notified when the List's outermost HTMLElement resizes. This may be used to (re)scroll a row into view. |
| onRowsRendered | Callback notified when the range of visible rows changes. |
| overscanCount | How many additional rows to render outside of the visible area. This can reduce visual flickering near the edges of a list when scrolling. |
| rowKey | Lists use the row index as a ℹ️ Custom keys can ensure better UX for sortable or filterable lists, particularly if your row components are stateful. Refer to the React documentation for more info. ⚠️ This prop cannot be auto-memoized because it is called during render.
It is important to always |
| scrollElement | Element that scrolls the list, for lists that scroll along with other content (e.g. the page):
The list renders at its full height and only renders the rows visible within the scroll element's viewport. Its position within the scroll element is measured whenever it renders, scrolls, or resizes, and whenever content before it is resized, inserted, or removed (e.g. an image loading above the list).
ℹ️ While the scroll element is ⚠️ The list itself doesn't scroll, so its |
| tagName | Can be used to override the root HTML element rendered by the List component. The default value is "div", meaning that List renders an HTMLDivElement as its root. ⚠️ In most use cases the default ARIA roles are sufficient and this prop is not needed. |
Renders data with many rows and columns.
ℹ️ Unlike List rows, Grid cell sizes must be known ahead of time.
Either static sizes or something that can be derived (from the data in CellProps) without rendering.
| Name | Description |
|---|---|
| cellComponent | React component responsible for rendering a cell. This component will receive ℹ️ The prop types for this component are exported as |
| cellProps | Additional props to be passed to the cell-rendering component. Grid will automatically re-render cells when values in this object change. ⚠️ This object must not contain |
| columnCount | Number of columns to be rendered in the grid. |
| columnWidth | Column width; the following formats are supported:
ℹ️ Percentage widths can't be resolved during server rendering unless a |
| rowCount | Number of rows to be rendered in the grid. |
| rowHeight | Row height; the following formats are supported:
ℹ️ Percentage heights can't be resolved during server rendering unless a |
| Name | Description |
|---|---|
| className | CSS class name. |
| dir | Indicates the directionality of grid cells. ℹ️ The directionality of an ancestor element can't be detected during server rendering; specify this prop explicitly for server-rendered right-to-left grids. ℹ️ See HTML |
| style | Optional CSS properties. The grid of cells will fill the height and width defined by this style. |
| children | Additional content to be rendered within the grid (above cells). This property can be used to render things like overlays or tooltips. |
| columnKey | Grids use the column index as a ℹ️ Custom keys can ensure better UX for sortable or filterable grids, particularly if your cell components are stateful. Refer to the React documentation for more info. ⚠️ This prop cannot be auto-memoized because it is called during render.
It is important to always |
| defaultHeight | Default height of grid (in pixels) for initial render, before its actual height has been measured. ℹ️ Grids can't be measured during server rendering, so this value determines how many rows are server rendered.
It's recommended to set this to the expected height of the grid (or to specify a pixel height via the |
| defaultScrollLeft | Initial horizontal scroll offset (in pixels) of the grid. This value is only applied once, when the grid first mounts with at least one column. ℹ️ Server-rendered HTML can't be pre-scrolled, so server rendering always starts at the first column; the default scroll offset is applied once the grid hydrates. ⚠️ If the grid can't be scrolled that far when the offset is applied (e.g. it is hidden with |
| defaultScrollTop | Initial vertical scroll offset (in pixels) of the grid. This value is only applied once, when the grid first mounts with at least one row. ℹ️ Server-rendered HTML can't be pre-scrolled, so server rendering always starts at the first row; the default scroll offset is applied once the grid hydrates. ⚠️ If the grid can't be scrolled that far when the offset is applied (e.g. it is hidden with |
| defaultWidth | Default width of grid (in pixels) for initial render, before its actual width has been measured. ℹ️ Grids can't be measured during server rendering, so this value determines how many columns are server rendered.
It's recommended to set this to the expected width of the grid (or to specify a pixel width via the |
| gridRef | Imperative Grid API. ℹ️ The |
| onCellsRendered | Callback notified when the range of rendered cells changes. |
| onResize | Callback notified when the Grid's outermost HTMLElement resizes. This may be used to (re)scroll a cell into view. |
| overscanCount | How many additional rows/columns to render outside of the visible area. This can reduce visual flickering near the edges of a grid when scrolling. |
| rowKey | Grids use the row index as a ℹ️ Custom keys can ensure better UX for sortable or filterable grids, particularly if your cell components are stateful. Refer to the React documentation for more info. ⚠️ This prop cannot be auto-memoized because it is called during render.
It is important to always |
| scrollElement | Element that scrolls the grid, for grids that scroll along with other content (e.g. the page):
The grid renders at its full height and only renders the rows (and columns) visible within the scroll element's viewport. Its position within the scroll element is measured whenever it renders, scrolls, or resizes, and whenever content before it is resized, inserted, or removed (e.g. an image loading above the grid).
ℹ️ While the scroll element is ⚠️ The grid's |
| tagName | Can be used to override the root HTML element rendered by the Grid component. The default value is "div", meaning that Grid renders an HTMLDivElement as its root. ⚠️ In most use cases the default ARIA roles are sufficient and this prop is not needed. |