Virtualized Example
Material React Table has a built-in virtualization features (via @tanstack/react-virtual
) that allows you to render a large number of rows or columns 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 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 0 | Middle Name 0 | Last Name 0 | Email Address 0 | Phone Number 0 | Address 0 | Zip Code 0 | City 0 | |
---|---|---|---|---|---|---|---|---|---|
1 | Dejah | Selmer | Swaniawski | Maryjane_Beatty93@gmail.com | 1-524-959-3682 x47936 | 8789 Maida Squares | 18090-0478 | East Modestatown | |
2 | Jayde | Bennie | Schmitt | Braeden.Mosciski64@hotmail.com | 420.651.2731 x513 | 763 Garrett Grove | 57561 | North Artport | |
3 | Veda | Robb | Parisian | Adolf.Prohaska@yahoo.com | 427-313-0547 x367 | 902 Grant Grove | 55328 | Devanworth | |
4 | Andre | Tristian | Huel | Heather_Cruickshank@gmail.com | 741.318.3076 x916 | 93550 Bella Crossroad | 35397 | West Shayna | |
5 | Salvatore | Angie | Koss | Hilda.Berge@hotmail.com | 459-491-9294 | 537 Cherry Tree Close | 84661 | Hellerberg | |
6 | Noemie | Ally | Kuhlman | Novella81@yahoo.com | 1-749-327-6265 x27913 | 45423 Stiedemann Rapids | 94716 | East Alberta | |
7 | Randi | Chester | Mohr | Kennith.Reinger@gmail.com | (553) 975-2463 x3869 | 771 Koss Mount | 65157-5916 | Hoegerborough | |
8 | Terry | Elwin | Volkman | Paxton.Romaguera@hotmail.com | 1-844-990-5841 x3001 | 6321 Dooley Hills | 45124-1842 | South Anselland | |
9 | Ross | Dalton | Terry | Stone.Kulas@hotmail.com | 256-965-1157 | 22516 Doyle Heights | 01395 | Priceburgh | |
10 | Brice | Wendy | Sauer | Gerry.Heller35@hotmail.com | 816-977-8349 | 5493 S Water Street | 16326 | O'Keefeton | |
11 | Beatrice | Allison | Denesik | Julia.Barton99@hotmail.com | 735.441.7981 x052 | 788 Witting Junctions | 19464-7572 | East Mavismouth | |
12 | Madaline | Marge | Blick | Susanna30@yahoo.com | 1-358-344-1623 | 24834 Vivien Key | 64477-2767 | Roweburgh | |
13 | Ana | Angie | Wiegand | Andre44@gmail.com | (648) 729-1112 x1397 | 89961 Mallory Harbors | 05358-6651 | Leonorastad | |
14 | Russel | Alexane | Friesen | Lizeth5@yahoo.com | 737.413.1425 | 84362 The Poplars | 40943 | Lake William | |
15 | Vivienne | Stephen | Reichel | Abdiel98@hotmail.com | 414-205-0564 | 77724 Ida Highway | 60011-8653 | Oakland | |
16 | Nola | Aglae | Gerhold | Asa97@yahoo.com | 518-745-3157 x08718 | 589 Cali Ridges | 22677-6014 | Robertport |
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...90 );9192 //optionally access the underlying virtualizer instance93 const rowVirtualizerInstanceRef = useRef<MRT_RowVirtualizer>(null);9495 const [data, setData] = useState<Person[]>([]);96 const [isLoading, setIsLoading] = useState(true);97 const [sorting, setSorting] = useState<MRT_SortingState>([]);9899 useEffect(() => {100 if (typeof window !== 'undefined') {101 setData(makeData(10_000));102 setIsLoading(false);103 }104 }, []);105106 useEffect(() => {107 //scroll to the top of the table when the sorting changes108 try {109 rowVirtualizerInstanceRef.current?.scrollToIndex?.(0);110 } catch (error) {111 console.error(error);112 }113 }, [sorting]);114115 const table = useMaterialReactTable({116 columns,117 data, //10,000 rows118 defaultDisplayColumn: { enableResizing: true },119 enableBottomToolbar: false,120 enableColumnResizing: true,121 enableColumnVirtualization: true,122 enableGlobalFilterModes: true,123 enablePagination: false,124 enableColumnPinning: true,125 enableRowNumbers: true,126 enableRowVirtualization: true,127 muiTableContainerProps: { sx: { maxHeight: '600px' } },128 onSortingChange: setSorting,129 state: { isLoading, sorting },130 rowVirtualizerInstanceRef, //optional131 rowVirtualizerOptions: { overscan: 5 }, //optionally customize the row virtualizer132 columnVirtualizerOptions: { overscan: 2 }, //optionally customize the column virtualizer133 });134135 return <MaterialReactTable table={table} />;136};137138export default Example;139
View Extra Storybook Examples