MRT logoMantine React Table

On This Page

    Virtualization Feature Guide

    Virtualization is useful when you have a lot of data you want to display client-side all at once, without having to use pagination. Mantine 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 Table Options

    #
    Prop Name
    Type
    Default Value
    More Info Links
    1MutableRefObject<Virtualizer | null>
    2Partial<VirtualizerOptions<HTMLDivElement, HTMLTableCellElement>>
    3booleanMRT Virtualization Docs
    4booleanMRT Virtualization Docs
    5MutableRefObject<Virtualizer | null>
    6Partial<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 table option to true. However, you will probably also want to turn off pagination, which you can do by setting enablePagination to false.

    const table = useMantineReactTable({
      columns,
      data,
      enablePagination: false, //turn off pagination
      enableRowVirtualization: true, //enable row virtualization
    });

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

    #
    First Name
    Middle Name
    Last Name
    Email Address
    Address
    Zip Code
    City
    State
    Country
    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useEffect, useMemo, useRef, useState } from 'react';
    import {
      MantineReactTable,
      useMantineReactTable,
      type MRT_ColumnDef,
      type MRT_SortingState,
      type MRT_RowVirtualizer,
    } from 'mantine-react-table';
    import { makeData, type Person } from './makeData';
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
            size: 150,
          },
          {
            accessorKey: 'middleName',
            header: 'Middle Name',
            size: 150,
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
            size: 150,
          },
          {
            accessorKey: 'email',
            header: 'Email Address',
            size: 300,
          },
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'zipCode',
            header: 'Zip Code',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
          {
            accessorKey: 'state',
            header: 'State',
          },
          {
            accessorKey: 'country',
            header: 'Country',
          },
        ],
        [],
      );
    
      //optionally access the underlying virtualizer instance
      const rowVirtualizerInstanceRef = useRef<MRT_RowVirtualizer>(null);
    
      const [data, setData] = useState<Person[]>([]);
      const [isLoading, setIsLoading] = useState(true);
      const [sorting, setSorting] = useState<MRT_SortingState>([]);
    
      useEffect(() => {
        if (typeof window !== 'undefined') {
          setData(makeData(10_000));
          setIsLoading(false);
        }
      }, []);
    
      useEffect(() => {
        try {
          //scroll to the top of the table when the sorting changes
          rowVirtualizerInstanceRef.current?.scrollToIndex(0);
        } catch (e) {
          console.log(e);
        }
      }, [sorting]);
    
      const table = useMantineReactTable({
        columns,
        data, //10,000 rows
        enableBottomToolbar: false,
        enableGlobalFilterModes: true,
        enablePagination: false,
        enableRowNumbers: true,
        enableRowVirtualization: true,
        mantineTableContainerProps: { style: { maxHeight: '600px' } },
        onSortingChange: setSorting,
        state: { isLoading, sorting },
        rowVirtualizerOptions: { overscan: 8 }, //optionally customize the virtualizer
      });
    
      return <MantineReactTable table={table} />;
    };
    
    export default Example;

    Enable Column Virtualization

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

    const table = useMantineReactTable({
      columns,
      data,
      enableColumnVirtualization: true, //enable column virtualization
    });

    Take a look at the example below with 500 columns!

    Rows per page

    1-10 of 10

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useRef } from 'react';
    import {
      MantineReactTable,
      useMantineReactTable,
      type MRT_ColumnVirtualizer,
    } from 'mantine-react-table';
    import { fakeColumns, fakeData } from './makeData';
    
    const Example = () => {
      //optionally access the underlying virtualizer instance
      const columnVirtualizerInstanceRef = useRef<MRT_ColumnVirtualizer>(null);
    
      const table = useMantineReactTable({
        columnVirtualizerInstanceRef, //optional
        columnVirtualizerOptions: { overscan: 4 }, //optionally customize the virtualizer
        columns: fakeColumns, //500 columns
        data: fakeData,
        enableColumnVirtualization: true,
        enableColumnPinning: true,
        enableRowNumbers: true,
      });
    
      return <MantineReactTable table={table} />;
    };
    
    export default Example;

    WARNING: Do NOT 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 table options automatically get set internally.

    layoutMode Table Option

    The layoutMode table option 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 Table Option

    The enableStickyHeader table option 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 rowVirtualizerOptions and columnVirtualizerOptions table options. 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.

    const table = useMantineReactTable({
      columns,
      data,
      enableColumnVirtualization: true,
      enablePagination: false,
      enableRowVirtualization: true,
      columnVirtualizerOptions: {
        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
      },
      rowVirtualizerOptions: {
        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.

    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
        try {
          rowVirtualizerInstanceRef.current.scrollToIndex(0);
        } catch (error) {
          console.error(error);
        }
      }
    }, [sorting]);
    
    const table = useMantineReactTable({
      columns,
      data,
      enableColumnVirtualization: true,
      enablePagination: false,
      enableRowVirtualization: true,
      rowVirtualizerInstanceRef,
      columnVirtualizerInstanceRef,
    });
    
    return <MantineReactTable table={table} />;

    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