react-window vs react-virtualized vs react-infinite-scroller
React 虚拟化和无限滚动库
react-windowreact-virtualizedreact-infinite-scroller类似的npm包:

React 虚拟化和无限滚动库

在现代 Web 开发中,处理大量数据的显示是一个常见的挑战。React 虚拟化和无限滚动库提供了一种高效的方式来渲染长列表或大数据集,优化性能并提升用户体验。这些库通过只渲染可视区域内的元素,减少了 DOM 操作的数量,从而提高了应用程序的响应速度和流畅度。它们各自有不同的特性和适用场景,开发者可以根据项目需求选择合适的库。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
react-window4,657,63417,102209 kB114 天前MIT
react-virtualized1,496,42327,0772.24 MB11 年前MIT
react-infinite-scroller03,31030.3 kB98-MIT

功能对比: react-window vs react-virtualized vs react-infinite-scroller

性能优化

  • react-window:

    react-window 是 react-virtualized 的轻量级替代方案,专注于性能和简洁性。它通过减少内存占用和提高渲染速度,适合需要高效渲染的场景。

  • react-virtualized:

    react-virtualized 提供了高度优化的虚拟化技术,能够处理数千个列表项而不会显著影响性能。它通过动态计算可见区域内的元素,确保只渲染必要的 DOM 元素。

  • react-infinite-scroller:

    react-infinite-scroller 通过只加载当前视口内的内容来优化性能,避免一次性加载所有数据,从而减少了初始渲染时间。

易用性

  • react-window:

    react-window 的 API 设计简洁,易于理解,适合快速实现虚拟化功能,适合初学者和需要快速开发的项目。

  • react-virtualized:

    react-virtualized 的 API 较为复杂,学习曲线较陡,但提供了更强大的功能和灵活性,适合需要高度自定义的应用。

  • react-infinite-scroller:

    react-infinite-scroller 提供了简单的 API,易于上手,适合快速开发和小型项目。它的配置选项少,适合不需要复杂功能的场景。

功能丰富性

  • react-window:

    react-window 提供基本的虚拟化功能,适合简单的列表和网格布局,功能相对较少,但在性能上表现优异。

  • react-virtualized:

    react-virtualized 提供了丰富的功能,包括列表、表格、网格等多种数据展示形式,支持复杂的自定义渲染,适合大型应用。

  • react-infinite-scroller:

    react-infinite-scroller 主要关注于无限滚动,功能相对简单,适合基本的列表展示。

社区支持

  • react-window:

    react-window 的社区也在不断增长,文档清晰易懂,适合快速上手和使用。

  • react-virtualized:

    react-virtualized 拥有活跃的社区和丰富的文档,提供了大量的示例和支持,适合需要深入学习的开发者。

  • react-infinite-scroller:

    react-infinite-scroller 的社区相对较小,文档和示例较少,但对于简单的需求来说足够使用。

适用场景

  • react-window:

    适合需要高性能和轻量级解决方案的项目,尤其是在性能要求高的场景下。

  • react-virtualized:

    适合需要处理大量数据的复杂应用,支持多种数据展示方式,适合企业级应用。

  • react-infinite-scroller:

    适合需要快速实现无限滚动的项目,尤其是小型应用和简单列表。

如何选择: react-window vs react-virtualized vs react-infinite-scroller

  • react-window:

    选择 react-window 如果你需要轻量级的虚拟化解决方案,适合简单的列表和网格布局。它比 react-virtualized 更小,性能更高,适合对性能要求较高的场景。

  • react-virtualized:

    选择 react-virtualized 如果你需要更复杂的虚拟化功能,支持多种数据展示形式(如表格、列表等),并且需要自定义渲染。它适合大型应用程序,提供了丰富的 API 和灵活性。

  • react-infinite-scroller:

    选择 react-infinite-scroller 如果你需要简单的无限滚动实现,适合快速集成和小型项目。它提供了基本的无限滚动功能,易于使用。

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; version 1.x documentation can be found at react-window-v1.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 grid's current height (string)
  • function that returns the row height (in pixels) given an index and cellProps
  • dynamic row height cache returned by the useDynamicRowHeight hook

⚠️ 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 cells). This property can be used to render things like overlays or tooltips.

defaultHeight

Default height of list for initial render. This value is important for server rendering.

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

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 an index and style prop 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
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

Optional props

NameDescription
className

CSS class name.

dir

Indicates the directionality of grid cells.

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

defaultHeight

Default height of grid for initial render. This value is important for server rendering.

defaultWidth

Default width of grid for initial render. This value is important for server rendering.

gridRef

Imperative Grid API.

ℹ️ The useGridRef and useGridCallbackRef hooks are exported for convenience use 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.

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.