react-window vs react-virtualized vs react-virtual
React 大型列表虚拟化方案对比
react-windowreact-virtualizedreact-virtual类似的npm包:

React 大型列表虚拟化方案对比

react-virtualized、react-window 和 react-virtual 都是为了解决 React 应用中渲染大量列表数据时的性能问题而设计的库。它们通过“虚拟化”技术,只渲染用户可见区域的 DOM 节点,从而大幅降低内存占用和渲染时间。react-virtualized 是早期的标准方案,功能全面但较重;react-window 是其继任者,更轻量且专注;react-virtual 则提供了基于 Hook 的无头(headless)方案,灵活性最高。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
react-window8,413,68617,200419 kB18 小时前MIT
react-virtualized2,311,88927,0671.31 MB112 天前MIT
react-virtual654,6957,178158 kB84-MIT

React 大型列表虚拟化:react-virtualized vs react-window vs react-virtual

在 React 开发中,当需要渲染成千上万条数据时,直接渲染会导致页面卡顿甚至崩溃。这三个库都通过“虚拟化”技术解决了这个问题 —— 它们只渲染用户可见区域内的元素,其余部分用占位符代替。虽然目标一致,但它们的设计理念和适用场景却有明显差异。

🏗️ 核心架构:组件封装 vs 逻辑钩子

这三个库最大的区别在于它们如何暴露虚拟化逻辑给开发者。

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)提供内容。

  • 组件只负责计算可见区域和样式。
  • 你负责渲染具体的 DOM 结构。
  • 平衡了易用性和灵活性。
// 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 获取虚拟化数据。
  • 你需要自己创建滚动容器和映射列表。
  • 灵活性最高,完全无头(headless)。
// 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 强制使用绝对定位。

  • 内部样式逻辑固定,难以覆盖。
  • 如果需要使用 Flexbox 或 Grid 布局内部元素,会遇到困难。
  • 适合标准列表,不适合复杂卡片布局。
// 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 数据,自己决定怎么写样式。
  • 可以轻松切换为 Grid 布局或混合布局。
  • 适合需要高度定制视觉效果的场景。
// react-virtual: 手动应用样式
<div
  style={{
    position: 'absolute',
    top: 0,
    left: 0,
    transform: `translate3d(0, ${item.start}px, 0)`,
  }}
>
  {item.index}
</div>

🔄 维护状态与生态建议

选择库时,维护状态是一个关键因素,它关系到未来的升级成本和安全性。

react-virtualized 已进入维护模式。

  • 作者建议新项目使用 react-window。
  • 修复 Bug 的速度较慢,主要支持现有用户。
  • 如果新项目没有特殊理由,不建议选用。

react-window 处于稳定维护期。

  • 由 React 核心团队成员维护,可靠性高。
  • API 稳定,很少破坏性更新。
  • 是大多数通用场景的默认推荐。

react-virtual 活跃开发中。

  • 属于 TanStack 生态系统,更新频繁。
  • 紧跟 React 新特性(如并发模式)。
  • 适合喜欢尝试新技术和需要深度定制的团队。

📊 核心特性对比表

特性react-virtualizedreact-windowreact-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 vs react-virtualized vs react-virtual

  • react-window:

    如果你需要快速实现标准的长列表或网格,且希望拥有稳定的维护和较小的包体积,react-window 是最佳选择。它由 react-virtualized 的同一作者开发,专为现代 React 优化,API 简洁,适合大多数通用的虚拟化场景。

  • react-virtualized:

    如果你的项目是旧代码库维护,且已经深度依赖 react-virtualized 的复杂组件(如 Table 或 Grid),继续使用它可以避免大规模重构。但在新项目中不建议选用,因为它主要基于 Class 组件,API 较重,且官方已建议转向更轻量的方案。

  • react-virtual:

    如果你需要极高的灵活性,希望完全控制 DOM 结构,或者正在使用 TanStack 生态系统,react-virtual 是理想方案。它基于 Hook 设计,无头(headless)特性让你可以自由组合样式和布局,非常适合需要定制化滚动行为或复杂布局的场景。

react-window的README

react-window logo

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.

Support

If you like this project there are several ways to support it:

The following wonderful companies and individuals have sponsored react-window:

Installation

Begin by installing the library from NPM:

npm install react-window

TypeScript types

TypeScript definitions are included within the published dist folder

FAQs

Frequently asked questions can be found here.

Documentation

Documentation for this project is available at react-window.vercel.app.

List

Renders data with many rows.

Required props

NameDescription
rowComponent

React component responsible for rendering a row.

This component will receive an index and style prop by default. Additionally it will receive prop values passed to rowProps.

ℹ️ The prop types for this component are exported as RowComponentProps

rowCount

Number of items to be rendered in the list.

rowHeight

Row height; the following formats are supported:

  • number of pixels (number)
  • percentage of the list's current height (string)
  • function that returns the row height (in pixels) given an index and rowProps
  • dynamic row height cache returned by the useDynamicRowHeight hook

ℹ️ Percentage heights can't be resolved during server rendering unless a defaultHeight (or a pixel height via the style prop) is specified.

⚠️ 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 ariaAttributes, index, or style props.

Optional props

NameDescription
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 style prop).

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 display: none, or uses percentage row heights before its height has been measured), the offset is clamped or ignored and not retried. Specify a defaultHeight (or a pixel height via the style prop) when using percentage row heights.

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 useListRef and useListCallbackRef hooks are exported for convenience in TypeScript projects.

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 key by default. This prop can provide a custom key value.

ℹ️ 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 useCallback for this prop; do not use an inline function.

scrollElement

Element that scrolls the list, for lists that scroll along with other content (e.g. the page):

  • "window" to scroll with the page
  • an ancestor element that scrolls (e.g. stored with a useState ref callback); null until it has mounted

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).

  • scrollToRow scrolls the scroll element
  • defaultHeight is used as the viewport height until the scroll element has been measured (e.g. during server rendering)
  • percentage row heights are relative to the scroll element's viewport height
  • defaultScrollOffset is ignored

ℹ️ While the scroll element is null, the list renders the first rows (as it does during server rendering).

⚠️ The list itself doesn't scroll, so its onScroll handler isn't called; listen to the scroll element's "scroll" events instead (or use the useIsScrolling hook).

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.

Grid

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.

Required props

NameDescription
cellComponent

React component responsible for rendering a cell.

This component will receive columnIndex, rowIndex, and style props by default. Additionally it will receive prop values passed to cellProps.

ℹ️ The prop types for this component are exported as CellComponentProps

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 ariaAttributes, columnIndex, rowIndex, or style props.

columnCount

Number of columns to be rendered in the grid.

columnWidth

Column width; the following formats are supported:

  • number of pixels (number)
  • percentage of the grid's current width (string)
  • function that returns the column width (in pixels) given an index and cellProps

ℹ️ Percentage widths can't be resolved during server rendering unless a defaultWidth (or a pixel width via the style prop) is specified.

rowCount

Number of rows to be rendered in the grid.

rowHeight

Row height; the following formats are supported:

  • number of pixels (number)
  • percentage of the grid's current height (string)
  • function that returns the row height (in pixels) given an index and cellProps

ℹ️ Percentage heights can't be resolved during server rendering unless a defaultHeight (or a pixel height via the style prop) is specified.

Optional props

NameDescription
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 dir global attribute for more information.

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 key by default. This prop can be used along with the rowKey prop to provide a custom key value.

ℹ️ 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 useCallback for this prop; do not use an inline function.

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 style prop).

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 display: none, or uses percentage column widths before its width has been measured), the offset is clamped or ignored and not retried. Specify a defaultWidth (or a pixel width via the style prop) when using percentage column widths.

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 display: none, or uses percentage row heights before its height has been measured), the offset is clamped or ignored and not retried. Specify a defaultHeight (or a pixel height via the style prop) when using percentage row heights.

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 style prop).

gridRef

Imperative Grid API.

ℹ️ The useGridRef and useGridCallbackRef hooks are exported for convenience in TypeScript projects.

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 key by default. This prop can be used along with the columnKey prop to provide a custom key value.

ℹ️ 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 useCallback for this prop; do not use an inline function.

scrollElement

Element that scrolls the grid, for grids that scroll along with other content (e.g. the page):

  • "window" to scroll with the page
  • an ancestor element that scrolls (e.g. stored with a useState ref callback); null until it has mounted

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).

  • columns scroll with the scroll element too if the grid is as wide as its content (e.g. style={{ width: "max-content" }}); otherwise the grid fills the width of its parent and its columns scroll within it
  • scrollToCell, scrollToColumn and scrollToRow scroll whichever element scrolls each axis
  • defaultHeight and defaultWidth are used as the viewport size until the scroll element has been measured (e.g. during server rendering)
  • percentage row heights and column widths are relative to the scroll element's viewport
  • defaultScrollLeft and defaultScrollTop are ignored

ℹ️ While the scroll element is null, the grid renders the first rows (as it does during server rendering).

⚠️ The grid's onScroll handler is only called when the grid scrolls its own columns; listen to the scroll element's "scroll" events instead (or use the useIsScrolling hook).

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.