Row Virtualization Example
Material React Table has a built-in row virtualization feature (via @tanstack/react-virtual
) that allows you to render a large number of rows without major performance issues that you would normally see with a large number of DOM elements.
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 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 0 | Middle Name 0 | Last Name 0 | Email Address 0 | Address 0 | Zip Code 0 | City 0 | State 0 | Country 0 |
---|---|---|---|---|---|---|---|---|---|
1 | Abraham | Nora | Hodkiewicz | Monique61@gmail.com | 174 Cronin Summit | 92122-0996 | Jakaylaport | New Hampshire | Cambodia |
2 | Alda | George | Mayert | Edwardo27@gmail.com | 97934 Roman Road | 30009 | Johathanmouth | Washington | Ecuador |
3 | Julien | Lina | Thiel | Maureen.Schuster@hotmail.com | 6664 Damon Pines | 14117-1951 | South Leonside | Illinois | Wallis and Futuna |
4 | Queen | Hayden | O'Reilly | Lily20@yahoo.com | 15541 Kulas Brooks | 47763 | Port Albertberg | Washington | Micronesia |
5 | Elena | Yasmine | Johnston | Nestor.Cummings@gmail.com | 33300 S Main Street | 36471 | Sonyaburgh | Mississippi | Bouvet Island |
6 | Lynn | Brice | Fritsch | Grayson.Shields@yahoo.com | 9632 Upton Terrace | 44893 | North Sashaberg | Maine | Iraq |
7 | Edwina | Melissa | Reinger | Karlee86@hotmail.com | 4773 Hoeger Stream | 87028 | Olenfort | Wyoming | American Samoa |
8 | Vaughn | Easter | Jakubowski | Mikayla.Klocko@gmail.com | 8635 E 9th Street | 91321 | Myramouth | Indiana | Saint Barthelemy |
9 | Royce | Sandra | Toy | Kyleigh_Conn@gmail.com | 3203 Cormier Groves | 32432 | Miramar | Minnesota | Ireland |
10 | Shanie | Flo | Dibbert | Gunnar83@hotmail.com | 594 Pacocha Club | 24613 | Kosston | Utah | Liechtenstein |
11 | Jocelyn | Nya | Denesik | Lauren_Koss@hotmail.com | 3717 Old Lane | 09429-5982 | North Kaitlyntown | Nevada | Saint Helena |
12 | Waylon | Craig | Conn | Adrian83@yahoo.com | 615 Madge River | 49651-9976 | South Colten | Iowa | Yemen |
13 | Donnell | Frederik | Konopelski | Koby.Schroeder@yahoo.com | 797 Waters Courts | 01782-1522 | Mannburgh | New Jersey | Mozambique |
14 | Macy | Raymond | Nikolaus | Ben.Cormier36@hotmail.com | 13251 Marcella Ports | 49010 | Lake Rosellaborough | South Dakota | Greenland |
15 | Daija | Caroline | Frami | Mollie_Torphy45@hotmail.com | 728 Kreiger Manor | 29028 | New Forestfurt | Wisconsin | Holy See (Vatican City State) |
16 | Davonte | Roslyn | Koelpin | Annie_Hudson35@yahoo.com | 997 Hipolito Lodge | 69123-2706 | East Anaisfurt | South Carolina | Bouvet Island |
1import { useEffect, useMemo, useRef, useState } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6 type MRT_SortingState,7 type MRT_RowVirtualizer,8} from 'material-react-table';9import { makeData, type Person } from './makeData';1011const Example = () => {12 const columns = useMemo<MRT_ColumnDef<Person>[]>(13 //column definitions...58 );5960 //optionally access the underlying virtualizer instance61 const rowVirtualizerInstanceRef = useRef<MRT_RowVirtualizer>(null);6263 const [data, setData] = useState<Person[]>([]);64 const [isLoading, setIsLoading] = useState(true);65 const [sorting, setSorting] = useState<MRT_SortingState>([]);6667 useEffect(() => {68 if (typeof window !== 'undefined') {69 setData(makeData(10_000));70 setIsLoading(false);71 }72 }, []);7374 useEffect(() => {75 //scroll to the top of the table when the sorting changes76 try {77 rowVirtualizerInstanceRef.current?.scrollToIndex?.(0);78 } catch (error) {79 console.error(error);80 }81 }, [sorting]);8283 const table = useMaterialReactTable({84 columns,85 data, //10,000 rows86 enableBottomToolbar: false,87 enableGlobalFilterModes: true,88 enablePagination: false,89 enableRowNumbers: true,90 enableRowVirtualization: true,91 muiTableContainerProps: { sx: { maxHeight: '600px' } },92 onSortingChange: setSorting,93 state: { isLoading, sorting },94 rowVirtualizerInstanceRef, //optional95 rowVirtualizerOptions: { overscan: 5 }, //optionally customize the row virtualizer96 });9798 return <MaterialReactTable table={table} />;99};100101export default Example;102
View Extra Storybook Examples