Async Loading Feature Guide
While you are fetching your data, you may want to show some loading indicators. Material React Table has some nice loading UI features built in that look better than a simple spinner.
This guide is mostly focused on the loading UI features. Make sure to also check out the Remote Data and React Query examples for server-side logic examples.
Relevant Props
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| Material UI LinearProgress Props | |||
2 |
| Material UI Skeleton Props | |||
Relevant State Options
isLoading UI
Rather than coding your own spinner or loading indicator, you can simply set the isLoading
state to true
, and Material React Table will show progress bars and cell skeletons for you.
<MaterialReactTablecolumns={columns}data={data ?? []} //fallback to array if data is undefinedstate={{ isLoading: true }}/>
First Name | Last Name | Email | City |
---|---|---|---|
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { Person } from './makeData';45const Example: FC = () => {6 const columns = useMemo<MRT_ColumnDef<Person>[]>(7 //column definitions...28 );2930 return (31 <MaterialReactTable32 columns={columns}33 data={[]}34 state={{ isLoading: true }}35 />36 );37};3839export default Example;40
Only Show Progress Bars or Skeletons
If you do not want both progress bars and cell skeletons to show, you can use the showProgressBars
and showSkeletons
states, instead.
<MaterialReactTablecolumns={columns}data={data ?? []} //fallback to array if data is undefinedstate={{ showProgressBars: true }} //or showSkeletons/>
Customize Linear Progress Bars
You can customize the linear progress bars by passing props to the muiLinearProgressProps
prop.
First Name | Last Name | Email | City |
---|---|---|---|
Dylan | Murray | dmurray@yopmail.com | East Daphne |
Raquel | Kohler | rkholer33@yopmail.com | Columbus |
Ervin | Reinger | ereinger@mailinator.com | South Linda |
Brittany | McCullough | bmccullough44@mailinator.com | Lincoln |
Branson | Frami | bframi@yopmain.com | New York |
Kevin | Klein | kklien@mailinator.com | Nebraska |
1import React, { FC, useEffect, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { data, Person } from './makeData';4import { Button } from '@mui/material';56const Example: FC = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 //column definitions...29 );3031 const [progress, setProgress] = useState(0);3233 //simulate random progress for demo purposes34 useEffect(() => {35 const interval = setInterval(() => {36 setProgress((oldProgress) => {37 const newProgress = Math.random() * 20;38 return Math.min(oldProgress + newProgress, 100);39 });40 }, 1000);41 return () => clearInterval(interval);42 }, []);4344 return (45 <MaterialReactTable46 columns={columns}47 data={data}48 muiLinearProgressProps={({ isTopToolbar }) => ({49 color: 'secondary',50 variant: 'determinate', //if you want to show exact progress value51 value: progress, //value between 0 and 10052 sx: {53 display: isTopToolbar ? 'block' : 'none', //hide bottom progress bar54 },55 })}56 renderTopToolbarCustomActions={() => (57 <Button onClick={() => setProgress(0)} variant="contained">58 Reset59 </Button>60 )}61 state={{ showProgressBars: true }}62 />63 );64};6566export default Example;67
Full Loading and Server-Side Logic Example
Here is a copy of the full React Query example.
First Name | Last Name | Address | State | Phone Number |
---|---|---|---|---|
1import React, { FC, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { IconButton, Tooltip } from '@mui/material';4import RefreshIcon from '@mui/icons-material/Refresh';5import type {6 ColumnFiltersState,7 PaginationState,8 SortingState,9} from '@tanstack/react-table';10import {11 QueryClient,12 QueryClientProvider,13 useQuery,14} from '@tanstack/react-query';1516type UserApiResponse = {17 data: Array<User>;18 meta: {19 totalRowCount: number;20 };21};2223type User = {24 firstName: string;25 lastName: string;26 address: string;27 state: string;28 phoneNumber: string;29};3031const Example: FC = () => {32 const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);33 const [globalFilter, setGlobalFilter] = useState('');34 const [sorting, setSorting] = useState<SortingState>([]);35 const [pagination, setPagination] = useState<PaginationState>({36 pageIndex: 0,37 pageSize: 10,38 });3940 const { data, isError, isFetching, isLoading, refetch } =41 useQuery<UserApiResponse>(42 [43 'table-data',44 columnFilters,45 globalFilter,46 pagination.pageIndex,47 pagination.pageSize,48 sorting,49 ],50 async () => {51 const url = new URL(52 '/api/data',53 process.env.NODE_ENV === 'production'54 ? 'https://www.material-react-table.com'55 : 'http://localhost:3000',56 );57 url.searchParams.set(58 'start',59 `${pagination.pageIndex * pagination.pageSize}`,60 );61 url.searchParams.set('size', `${pagination.pageSize}`);62 url.searchParams.set('filters', JSON.stringify(columnFilters ?? []));63 url.searchParams.set('globalFilter', globalFilter ?? '');64 url.searchParams.set('sorting', JSON.stringify(sorting ?? []));6566 const response = await fetch(url.href);67 const json = (await response.json()) as UserApiResponse;68 return json;69 },70 { keepPreviousData: true },71 );7273 const columns = useMemo<MRT_ColumnDef<User>[]>(74 () => [75 {76 accessorKey: 'firstName',77 header: 'First Name',78 },79 {80 accessorKey: 'lastName',81 header: 'Last Name',82 },83 {84 accessorKey: 'address',85 header: 'Address',86 },87 {88 accessorKey: 'state',89 header: 'State',90 },91 {92 accessorKey: 'phoneNumber',93 header: 'Phone Number',94 },95 ],96 [],97 );9899 return (100 <MaterialReactTable101 columns={columns}102 data={data?.data ?? []} //data is undefined on first render103 initialState={{ showColumnFilters: true }}104 manualFiltering105 manualPagination106 manualSorting107 muiToolbarAlertBannerProps={108 isError109 ? {110 color: 'error',111 children: 'Error loading data',112 }113 : undefined114 }115 onColumnFiltersChange={setColumnFilters}116 onGlobalFilterChange={setGlobalFilter}117 onPaginationChange={setPagination}118 onSortingChange={setSorting}119 renderTopToolbarCustomActions={() => (120 <Tooltip arrow title="Refresh Data">121 <IconButton onClick={() => refetch()}>122 <RefreshIcon />123 </IconButton>124 </Tooltip>125 )}126 rowCount={data?.meta?.totalRowCount ?? 0}127 state={{128 columnFilters,129 globalFilter,130 isLoading,131 pagination,132 showAlertBanner: isError,133 showProgressBars: isFetching,134 sorting,135 }}136 />137 );138};139140const queryClient = new QueryClient();141142const ExampleWithReactQueryProvider = () => (143 <QueryClientProvider client={queryClient}>144 <Example />145 </QueryClientProvider>146);147148export default ExampleWithReactQueryProvider;149