MRT logoMaterial React Table

    Virtualization Feature Guide

    MRT v1.4 and v1.5 have major virtualization upgrades after switching to @tanstack/react-virtual v3.0!

    Virtualization is useful when you have a lot of data you want to display client-side all at once, without having to use pagination. Material React Table makes this as simple as possible, thanks to @tanstack/react-virtual with both row virtualization and column virtualization support.

    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 less 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.

    Relevant Props

    1
    MutableRefObject<Virtualizer | null>
    2
    Partial<VirtualizerOptions<HTMLDivElement, HTMLTableCellElement>>
    3
    boolean
    MRT Virtualization Docs
    4
    boolean
    MRT Virtualization Docs
    5
    MutableRefObject<Virtualizer | null>
    6
    Partial<VirtualizerOptions<HTMLDivElement, HTMLTableRowElement>>

    What is Virtualization?

    Virtualization, or virtual scrolling, works by only rendering the rows or columns that are visible on the screen. This is useful for performance and user experience, as we can make it appear that there are hundreds, thousands, or even tens of thousands of rows in the table all at once, but in reality, the table will only render the couple dozen rows that are visible on the screen, or the handful of columns that are visible on the screen.

    For more reading on the concept of virtualization, we recommend this blog post by LogRocket.

    Does Your Table Even Need Virtualization?

    If your table is paginated or you are not going to render more than 50 rows at once, you probably do not need row virtualization.

    If your table does not have over 12 columns, you probably do not need column virtualization.

    There is a tiny bit of extra overhead that gets added to your table's rendering when virtualization is enabled, so do not just enable it for every table. That being said, if your table does have well over 100 rows that it is trying to render all at once without pagination, performance will be night and day once it is enabled.

    Enable Row Virtualization

    Enabling row virtualization is as simple as setting the enableRowVirtualization prop to true. However, you will probably also want to turn off pagination, which you can do by setting enablePagination to false.

    <MaterialReactTable
    columns={columns}
    data={data}
    enablePagination={false}
    enableRowVirtualization
    />

    Take a look at the example below with 10,000 rows!


    Demo

    Open StackblitzOpen Code SandboxOpen on GitHub

    Source Code

    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';
    6
    7const Example: FC = () => {
    8 const columns = useMemo<MRT_ColumnDef<Person>[]>(
    9 //column definitions...
    54 );
    55
    56 //optionally access the underlying virtualizer instance
    57 const rowVirtualizerInstanceRef =
    58 useRef<Virtualizer<HTMLDivElement, HTMLTableRowElement>>(null);
    59
    60 const [data, setData] = useState<Person[]>([]);
    61 const [isLoading, setIsLoading] = useState(true);
    62 const [sorting, setSorting] = useState<SortingState>([]);
    63
    64 useEffect(() => {
    65 if (typeof window !== 'undefined') {
    66 setData(makeData(10_000));
    67 setIsLoading(false);
    68 }
    69 }, []);
    70
    71 useEffect(() => {
    72 //scroll to the top of the table when the sorting changes
    73 rowVirtualizerInstanceRef.current?.scrollToIndex(0);
    74 }, [sorting]);
    75
    76 return (
    77 <MaterialReactTable
    78 columns={columns}
    79 data={data} //10,000 rows
    80 enableBottomToolbar={false}
    81 enableGlobalFilterModes
    82 enablePagination={false}
    83 enableRowNumbers
    84 enableRowVirtualization
    85 muiTableContainerProps={{ sx: { maxHeight: '600px' } }}
    86 onSortingChange={setSorting}
    87 state={{ isLoading, sorting }}
    88 rowVirtualizerInstanceRef={rowVirtualizerInstanceRef} //optional
    89 rowVirtualizerProps={{ overscan: 8 }} //optionally customize the virtualizer
    90 />
    91 );
    92};
    93
    94//virtualizerInstanceRef was renamed to rowVirtualizerInstanceRef in v1.5.0
    95//virtualizerProps was renamed to rowVirtualizerProps in v1.5.0
    96
    97export default Example;
    98

    Enable Column Virtualization

    New in MRT v1.5!

    Enabling column virtualization is also as simple as setting the enableColumnVirtualization prop to true.

    <MaterialReactTable columns={columns} data={data} enableColumnVirtualization />

    Take a look at the example below with 500 columns!


    1KarleyAngelinaChristineLincoln
    2LilyanJoannyAlvinaPat
    3TerrillShaniyaCatalinaKariane
    4ElwinAkeemVerdieLaney
    5HelenKristofferLiamBruce
    6SusieJanetHalleMara
    7JasminTristinKristofferOwen
    8CameronJaydaFrancescaAlexandria
    9MackNelsonJeanneHouston
    10JonathonMitchelChelseyJodie

    Rows per page

    1-10 of 10

    Source Code

    1import React, { FC, useRef } from 'react';
    2import MaterialReactTable from 'material-react-table';
    3import type { Virtualizer } from '@tanstack/react-virtual';
    4import { fakeColumns, fakeData } from './makeData';
    5
    6const Example: FC = () => {
    7 //optionally access the underlying virtualizer instance
    8 const columnVirtualizerInstanceRef =
    9 useRef<Virtualizer<HTMLDivElement, HTMLTableCellElement>>(null);
    10
    11 return (
    12 <MaterialReactTable
    13 columnVirtualizerInstanceRef={columnVirtualizerInstanceRef} //optional
    14 columnVirtualizerProps={{ overscan: 4 }} //optionally customize the virtualizer
    15 columns={fakeColumns} //500 columns
    16 data={fakeData}
    17 enableColumnVirtualization
    18 enablePinning
    19 enableRowNumbers
    20 />
    21 );
    22};
    23
    24export default Example;
    25

    WARNING: Don't enable row or column virtualization conditionally. It may break React's Rule of Hooks, and/or cause other UI jumpiness.

    Virtualization Side Effects

    When either Row or Column Virtualization is enabled, a few other props automatically get set internally.

    layoutMode Prop

    In MRT Versions 1.3 and earlier, a CSS table-layout: fixed style was automatically added to the <table> element to prevent columns wiggling back and forth during scrolling due to body cells having variating widths.

    But now in MRT Versions 1.4 and later, the layoutMode prop is automatically set to the 'grid' value when either row or column virtualization is enabled, which means that all of the table markup will use CSS Grid and Flexbox instead of the traditional semantic styles that usually come with table tags. This is necessary to make the virtualization work properly with decent performance.

    enableStickyHeader Prop

    The enableStickyHeader prop is automatically set to true when row virtualization is enabled. This keeps the table header sticky and visible while scrolling and adds a default max-height of 100vh to the table container.

    Customize Virtualizer Props

    You can adjust some of the virtualizer props that are used internally with the rowVirtualizerProps and columnVirtualizerProps props. The most useful virtualizer props are the overscan and estimateSize options. You may want to adjust these values if you have unusual row heights or column widths that are causing the default scrolling to act weirdly.

    <MaterialReactTable
    columns={columns}
    data={data}
    enableColumnVirtualization
    enablePagination={false}
    enableRowVirtualization
    columnVirtualizerProps={{
    overscan: 5, //adjust the number of columns that are rendered to the left and right of the visible area of the table
    estimateSize: () => 400, //if your columns are wider or , try tweaking this value to make scrollbar size more accurate
    }}
    rowVirtualizerProps={{
    overscan: 10, //adjust the number or rows that are rendered above and below the visible area of the table
    estimateSize: () => 100, //if your rows are taller than normal, try tweaking this value to make scrollbar size more accurate
    }}
    />

    See the official TanStack Virtualizer Options API Docs for more information.

    MRT v1.4 upgraded from react-virtual v2 to @tanstack/react-virtual v3.0, which has some breaking changes and virtualizer option name changes. TypeScript hints should help you with any prop name changes, but you can also view the official TanStack Virtual Docs for guidance.

    Access Underlying Virtualizer Instances

    In a similar way that you can access the underlying table instance, you can also access the underlying virtualizer instances. This can be useful for accessing methods like the scrollToIndex method, which can be used to programmatically scroll to a specific row or column.

    const columnVirtualizerInstanceRef = useRef<Virtualizer>(null);
    const rowVirtualizerInstanceRef = useRef<Virtualizer>(null);
    useEffect(() => {
    if (rowVirtualizerInstanceRef.current) {
    //scroll to the top of the table when sorting changes
    rowVirtualizerInstanceRef.current.scrollToIndex(0);
    }
    }, [sorting]);
    return (
    <MaterialReactTable
    columns={columns}
    data={data}
    enableColumnVirtualization
    enableRowVirtualization
    rowVirtualizerInstanceRef={rowVirtualizerInstanceRef}
    columnVirtualizerInstanceRef={columnVirtualizerInstanceRef}
    />
    );

    See the official TanStack Virtualizer Instance API Docs for more information.

    Full Row and Column Virtualization Example

    Try out the performance of the fully virtualized example with 10,000 rows and over a dozen columns! Filtering, Search, and Sorting also maintain usable performance.

    View Extra Storybook Examples