Virtualized Example
Material React Table has a built-in row virtualization feature (via @tanstack/react-virual
) that lets you to render a large number of rows without major performance issues.
Try out the performance of the table below with 10,000 rows and over a dozen columns! Filtering, Search, and Sorting also maintain usable performance.
Be sure to also check out the full virtualization feature guide docs to learn about both Row and Column Virtualization.
NOTE: You should only enable row virtualization if you have a large number of rows or columns. Depending on the size of the table, if you are rendering fewer than a couple dozen rows at a time, you will actually just be adding extra overhead to the table renders. Virtualization only becomes necessary when you have over 50 rows or so at the same time with no pagination or dozens of columns.
# | First Name | Middle Name | |
---|---|---|---|
1import React, { FC, useEffect, useMemo, useRef, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import type { SortingState } from '@tanstack/react-table';4import type { Virtualizer } from '@tanstack/react-virtual';5import { makeData, Person } from './makeData';67const Example: FC = () => {8 const columns = useMemo<MRT_ColumnDef<Person>[]>(9 //column definitions...86 );8788 //optionally access the underlying virtualizer instance89 const rowVirtualizerInstanceRef =90 useRef<Virtualizer<HTMLDivElement, HTMLTableRowElement>>(null);9192 const [data, setData] = useState<Person[]>([]);93 const [isLoading, setIsLoading] = useState(true);94 const [sorting, setSorting] = useState<SortingState>([]);9596 useEffect(() => {97 if (typeof window !== 'undefined') {98 setData(makeData(10_000));99 setIsLoading(false);100 }101 }, []);102103 useEffect(() => {104 //scroll to the top of the table when the sorting changes105 rowVirtualizerInstanceRef.current?.scrollToIndex(0);106 }, [sorting]);107108 return (109 <MaterialReactTable110 columns={columns}111 data={data} //10,000 rows112 enableBottomToolbar={false}113 enableColumnVirtualization114 enableGlobalFilterModes115 enablePagination={false}116 enablePinning117 enableRowNumbers118 enableRowVirtualization119 muiTableContainerProps={{ sx: { maxHeight: '600px' } }}120 onSortingChange={setSorting}121 state={{ isLoading, sorting }}122 rowVirtualizerInstanceRef={rowVirtualizerInstanceRef} //optional123 rowVirtualizerProps={{ overscan: 5 }} //optionally customize the row virtualizer124 columnVirtualizerProps={{ overscan: 2 }} //optionally customize the column virtualizer125 />126 );127};128129//virtualizerInstanceRef was renamed to rowVirtualizerInstanceRef in v1.5.0130//virtualizerProps was renamed to rowVirtualizerProps in v1.5.0131132export default Example;133
View Extra Storybook Examples