react-window vs react-virtualized vs react-list vs react-tiny-virtual-list vs react-infinite
React 列表虚拟化方案深度对比与选型指南
react-windowreact-virtualizedreact-listreact-tiny-virtual-listreact-infinite類似套件:

React 列表虚拟化方案深度对比与选型指南

这些库旨在解决 React 应用中渲染大量数据时的性能瓶颈。react-virtualized 和 react-window 采用窗口化(windowing)技术,仅渲染可视区域内的 DOM 节点,大幅提升性能。react-infinite 侧重于无限加载逻辑,但通常不具备虚拟化能力。react-list 和 react-tiny-virtual-list 提供了介于两者之间的解决方案,分别在灵活性和体积上有所侧重。开发者需根据项目对性能、维护性及功能复杂度的需求进行选择。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
react-window8,481,69417,212428 kB15 天前MIT
react-virtualized2,262,03727,0671.31 MB119 天前MIT
react-list651,5131,97035.3 kB724 個月前MIT
react-tiny-virtual-list140,3272,483-558 年前MIT
react-infinite10,1032,682243 kB101-BSD-3-Clause

React 列表虚拟化方案深度对比:从 react-infinite 到 react-window

在构建数据密集型 React 应用时,列表渲染性能往往是首要瓶颈。当数据量达到成千上万条时,直接渲染所有 DOM 节点会导致页面卡顿甚至崩溃。本文对比的五款库 —— react-infinite、react-list、react-tiny-virtual-list、react-virtualized 和 react-window —— 提供了不同的解决思路。我们将深入探讨它们的核心机制、API 设计及维护状态,帮助你做出架构决策。

🏗️ 核心机制:无限加载 vs 窗口虚拟化

性能优化的核心在于减少 DOM 节点数量。

react-infinite 侧重于逻辑上的无限加载,而非 DOM 优化。

  • 它监听滚动事件触发数据加载回调。
  • 默认情况下,它倾向于保留已加载的 DOM 节点,数据量增大时性能会线性下降。
// react-infinite: 监听加载事件
<Infinite
  elementHeight={200}
  containerHeight={600}
  onInfiniteLoad={this.handleLoadMore}
>
  {items.map(item => <div key={item.id}>{item.name}</div>)}
</Infinite>

react-virtualized 和 react-window 采用真正的窗口化(Windowing)技术。

  • 它们只渲染可视区域及其缓冲区的节点。
  • 无论数据总量多少,DOM 节点数量保持恒定,性能稳定。
// react-virtualized: 使用 List 组件
<List
  width={300}
  height={600}
  rowCount={items.length}
  rowHeight={50}
  rowRenderer={({ index, style }) => (
    <div key={items[index].id} style={style}>{items[index].name}</div>
  )}
/>
// react-window: 使用 FixedSizeList 组件
<FixedSizeList
  height={600}
  itemCount={items.length}
  itemSize={50}
  width={300}
>
  {({ index, style }) => (
    <div key={items[index].id} style={style}>{items[index].name}</div>
  )}
</FixedSizeList>

react-list 和 react-tiny-virtual-list 也提供虚拟化。

  • react-list 允许更灵活的 item 渲染器配置。
  • react-tiny-virtual-list 专注于极简实现。
// react-list: 使用 itemRenderer
<List
  itemRenderer={this.renderItem}
  length={items.length}
  estimateItemSize={50}
/>
// react-tiny-virtual-list: 简洁 API
<VirtualList
  width={300}
  height={600}
  itemSize={50}
  items={items}
  renderItem={({ index, style }) => (
    <div key={items[index].id} style={style}>{items[index].name}</div>
  )}
/>

🛠️ API 设计:类组件 vs 现代 Hooks

API 的设计风格直接影响开发体验和代码可维护性。

react-virtualized 诞生于 React 类组件时代。

  • 大量依赖 ref 回调和类组件生命周期。
  • 与现代 Hooks 代码混合使用时略显繁琐。
// react-virtualized: 通常需要配合 ref 使用
class MyList extends React.Component {
  render() {
    return <List ref={this.listRef} ... />;
  }
}

react-window 为现代 React 而生。

  • 完美支持函数组件和 Hooks。
  • API 更直观,减少了样板代码。
// react-window: 原生支持函数组件
function MyList({ items }) {
  return (
    <FixedSizeList itemCount={items.length} ...>
      {({ index, style }) => <div style={style}>{items[index]}</div>}
    </FixedSizeList>
  );
}

react-infinite 同样基于较旧的 React 模式。

  • 配置项较为分散,往往需要手动管理容器高度。
// react-infinite: 配置较为繁琐
<Infinite
  useWindowScroll={true}
  containerHeight={this.state.height}
  ...
/>

react-list 和 react-tiny-virtual-list 介于两者之间。

  • react-list 提供稳定的 API,但配置项较多。
  • react-tiny-virtual-list 力求简单,但扩展性有限。
// react-tiny-virtual-list: 配置直观
<VirtualList
  items={items}
  renderItem={...}
/>

⚠️ 维护状态与弃用警告

这是选型中最关键的非技术因素。

react-virtualized 已不再积极维护。

  • 官方 README 明确建议新用户迁移至 react-window。
  • 虽然它功能强大(支持复杂的网格、表格、自动调整大小),但修复 bug 和新特性更新已停止。
  • 建议: 仅在维护旧代码库时使用,新项目请勿选用。

react-infinite 基本处于停滞状态。

  • 最后更新时间久远,不再适应现代 React 生态。
  • 缺乏虚拟化核心能力,不适合大数据量场景。
  • 建议: 避免使用,改用具备虚拟化能力的库。

react-window 是当前标准。

  • 由 react-virtualized 同一作者维护,活跃度高。
  • 社区支持好,文档完善。
  • 建议: 新项目默认首选。

react-list 和 react-tiny-virtual-list 状态稳定。

  • 适合特定需求,但社区活跃度不如 react-window。
  • 建议: 作为备选方案评估。

📐 功能覆盖:网格、可变高度与自动调整

不同场景需要不同的功能支持。

可变高度列表:

  • react-virtualized 使用 CellMeasurer 配合 List,配置复杂但功能强。
  • react-window 提供 VariableSizeList,API 更简洁,需手动通知尺寸变化。
  • react-list 通过 estimateItemSize 和重新渲染处理。
  • react-tiny-virtual-list 支持有限,通常需固定高度或简单估算。
  • react-infinite 不支持真正的可变高度虚拟化。
// react-window: 可变高度
<VariableSizeList
  itemSize={index => heights[index]}
  itemCount={items.length}
  ...
/>
// 需调用 listRef.resetAfterIndex(index) 通知更新
// react-virtualized: 可变高度
<CellMeasurerCache defaultHeight={100}>
  <List
    rowHeight={cache.rowHeight}
    rowRenderer={({ index, style, parent }) => (
      <CellMeasurer cache={cache} index={index} parent={parent}>
        {({ measure, didMeasure }) => (
          <div style={style} onLoad={measure}>...</div>
        )}
      </CellMeasurer>
    )}
  />
</CellMeasurerCache>

网格布局(Grid):

  • react-virtualized 和 react-window 均提供 Grid 组件。
  • react-list、react-tiny-virtual-list、react-infinite 主要专注于单维列表。
// react-window: 网格
<FixedSizeGrid
  columnCount={12}
  rowCount={100}
  columnWidth={100}
  rowHeight={50}
  ...
/>

📊 总结对比表

特性react-windowreact-virtualizedreact-listreact-tiny-virtual-listreact-infinite
虚拟化核心✅ 是✅ 是✅ 是✅ 是❌ 否 (通常)
维护状态🟢 活跃🔴 不再积极维护🟡 稳定🟡 稳定🔴 停滞
API 风格🪝 Hooks 友好🏛️ 类组件为主🛠️ 配置型🧸 极简🕰️ 旧式
可变高度✅ 支持✅ 支持 (复杂)✅ 支持⚠️ 有限❌ 不支持
网格支持✅ 支持✅ 支持❌ 否❌ 否❌ 否
推荐场景新项目首选旧项目维护特殊定制轻量场景不推荐

💡 架构师建议

在选择列表库时,请遵循以下原则:

  1. 默认选择 react-window:它是 react-virtualized 的精神继承者,更轻、更快、更符合现代 React 开发习惯。除非你有非常特殊的理由,否则不要偏离这个默认选项。

  2. 避免 react-virtualized 新接入:尽管它功能丰富,但维护状态已改变。引入它意味着未来可能需要重构迁移至 react-window,这会带来不必要的技术债务。

  3. 警惕 react-infinite 的性能陷阱:很多开发者误以为“无限加载”等于“性能优化”。实际上,如果没有虚拟化技术,加载 10,000 条数据依然会卡死浏览器。确保你选择的是具备“窗口化”能力的库。

  4. 评估复杂性:如果你的列表需要复杂的网格布局或交叉轴滚动,react-window 和 react-virtualized 是唯二的支持者。对于简单列表,react-tiny-virtual-list 可以作为轻量替代,但需权衡未来扩展性。

最终结论:现代 React 架构中,react-window 是列表虚拟化的事实标准。它平衡了性能、体积和开发体验,是大多数工程场景下的最优解。

如何選擇: react-window vs react-virtualized vs react-list vs react-tiny-virtual-list vs react-infinite

  • react-window:

    首选方案。如果你正在启动新项目,react-window 是最佳选择。它由 react-virtualized 的作者维护,专为现代 React 设计,支持 Hooks,体积更小且 API 更简洁。除非你有特殊的遗留需求,否则应优先考虑它。

  • react-virtualized:

    仅用于维护旧项目。该库已不再积极维护,官方明确建议新用户迁移至 react-window。虽然功能全面(支持网格、表格等),但包体积较大且 API 基于较旧的 React 模式,新项目应避免使用。

  • react-list:

    适用于需要高度自定义渲染逻辑的场景。如果你需要比 react-window 更细粒度的控制,且能接受稍复杂的配置,它是一个稳定的备选方案。但在大多数标准列表场景中,react-window 通常更易上手。

  • react-tiny-virtual-list:

    适用于对包体积极度敏感的小型项目。如果你只需要基础的虚拟列表功能,且希望依赖尽可能少,这是一个轻量级选择。但功能丰富度不如 react-window,复杂场景可能受限。

  • react-infinite:

    不推荐用于高性能场景。该库主要处理无限加载逻辑,往往渲染所有 DOM 节点,导致长列表性能急剧下降。仅适用于数据量极小且只需简单分页加载的遗留系统,现代应用应避开。

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.

ℹ️ Spread ariaAttributes onto the row element; they identify it as a row (e.g. so that dynamic row heights are measured reliably)

ℹ️ 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.