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! Filtering, Search, and Sorting also maintain usable performance.
Be sure to also check out the full row virtualization feature guide docs.
NOTE: You should only enable row virtualization if you have a large number of rows. 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.
# | First Name | Middle Name | Last Name | Email Address | Phone Number | Address | Zip Code | City | State | Country | Pet Name | Age |
---|---|---|---|---|---|---|---|---|---|---|---|---|
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...66 );6768 //optionally access the underlying virtualizer instance69 const virtualizerInstanceRef =70 useRef<Virtualizer<HTMLDivElement, HTMLTableRowElement>>(null);7172 const [data, setData] = useState<Person[]>([]);73 const [isLoading, setIsLoading] = useState(true);74 const [sorting, setSorting] = useState<SortingState>([]);7576 useEffect(() => {77 if (typeof window !== 'undefined') {78 setData(makeData(10_000));79 setIsLoading(false);80 }81 }, []);8283 useEffect(() => {84 if (virtualizerInstanceRef.current) {85 //scroll to the top of the table when the sorting changes86 virtualizerInstanceRef.current.scrollToIndex(0);87 }88 }, [sorting]);8990 return (91 <MaterialReactTable92 columns={columns}93 data={data} //10,000 rows94 enableBottomToolbar={false}95 enableGlobalFilterModes96 enablePagination={false}97 enableRowNumbers98 enableRowVirtualization99 muiTableContainerProps={{ sx: { maxHeight: '600px' } }}100 onSortingChange={setSorting}101 state={{ isLoading, sorting }}102 virtualizerInstanceRef={virtualizerInstanceRef} //optional103 virtualizerProps={{ overscan: 8 }} //optionally customize the virtualizer104 />105 );106};107108export default Example;109
View Extra Storybook Examples