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, { useEffect, useMemo, useRef, useState } from 'react';2import MaterialReactTable, {3 MRT_ColumnDef,4 MRT_SortingState,5 MRT_Virtualizer,6} from 'material-react-table';7import { makeData, Person } from './makeData';89const Example = () => {10 const columns = useMemo<MRT_ColumnDef<Person>[]>(11 //column definitions...88 );8990 //optionally access the underlying virtualizer instance91 const rowVirtualizerInstanceRef =92 useRef<MRT_Virtualizer<HTMLDivElement, HTMLTableRowElement>>(null);9394 const [data, setData] = useState<Person[]>([]);95 const [isLoading, setIsLoading] = useState(true);96 const [sorting, setSorting] = useState<MRT_SortingState>([]);9798 useEffect(() => {99 if (typeof window !== 'undefined') {100 setData(makeData(10_000));101 setIsLoading(false);102 }103 }, []);104105 useEffect(() => {106 //scroll to the top of the table when the sorting changes107 rowVirtualizerInstanceRef.current?.scrollToIndex(0);108 }, [sorting]);109110 return (111 <MaterialReactTable112 columns={columns}113 data={data} //10,000 rows114 defaultDisplayColumn={{ enableResizing: true }}115 enableBottomToolbar={false}116 enableColumnResizing117 enableColumnVirtualization118 enableGlobalFilterModes119 enablePagination={false}120 enablePinning121 enableRowNumbers122 enableRowVirtualization123 muiTableContainerProps={{ sx: { maxHeight: '600px' } }}124 onSortingChange={setSorting}125 state={{ isLoading, sorting }}126 rowVirtualizerInstanceRef={rowVirtualizerInstanceRef} //optional127 rowVirtualizerProps={{ overscan: 5 }} //optionally customize the row virtualizer128 columnVirtualizerProps={{ overscan: 2 }} //optionally customize the column virtualizer129 />130 );131};132133//virtualizerInstanceRef was renamed to rowVirtualizerInstanceRef in v1.5.0134//virtualizerProps was renamed to rowVirtualizerProps in v1.5.0135136export default Example;137
View Extra Storybook Examples