react-window README

The project's README from github.com/bvaughn/react-window. As one markdown file: README.md.

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

Name Description
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

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

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.

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

Name Description
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

Name Description
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.

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

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.

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.

react-window (bvaughn/react-window)

Searching Database…

这是什么?react-window 是一个 React 组件库,用于高效渲染大型列表和表格数据,避免一次性渲染大量 DOM 元素所带来的性能问题。
它是 UI 框架,还是只是一组组件库?它是一个轻量级库,主要包含两个组件:List 和 Grid。你可以把它直接放进自己的 React 应用中,而它本身并不是一个完整的框架。
如何把它添加到项目中?通过 npm 或 pnpm 将其作为依赖安装,然后导入 List 或 Grid 组件,并传入 rowComponent/cellComponent,以及行、列的数量和尺寸。
使用它需要什么特殊条件吗,比如 TypeScript?除了 React 本身,不需要其他特殊的运行时依赖。它使用 TypeScript 编写,并提供了类型化的属性,但纯 JavaScript 项目同样可以使用。
Grid 和 List 在行高设置上的用法一样吗?List 的行可以通过一个 Hook 使用动态高度,但 Grid 的单元格尺寸必须提前确定(静态设置或可推导出来),因为渲染之后无法再进行测量。
通常哪些人会用这类库?正在开发包含超大列表或表格的 React 应用的开发者,他们需要流畅的滚动体验,同时又不想渲染成千上万个视口之外的 DOM 节点。它已经被用于 React DevTools 和 Replay 浏览器中。
对于可排序或可筛选的列表,能否自定义行的 key?可以。List 和 Grid 都接受自定义的 rowKey/columnKey 属性,而不是依赖默认的基于索引的 key。这有助于在排序或筛选时,让带状态的行保持正确的交互表现。
能否更改列表渲染的 DOM 元素类型?可以。tagName 属性允许你覆盖根元素(默认是 div),适用于默认的 ARIA 角色不符合你使用场景的情况。
它是否支持服务端渲染?支持。List 和 Grid 都接受 defaultHeight 属性(Grid 还接受 defaultWidth),专门用于为服务端渲染的初始输出提供尺寸。
如何以命令式的方式操作列表或表格,例如滚动到某一项?使用 listRef 或 gridRef 属性(在 TypeScript 中可配合导出的 useListRef/useGridRef Hook),即可获得用于滚动以及访问外层 DOM 元素的命令式 API。
Creator
bvaughn
Size
9.1 MB
Stars
17.2K
Age
8y
Commits · 90 Days