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 |
---|---|---|---|
1-10 of 10
1import { useMemo } from 'react';2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { Person } from './makeData';45const Example = () => {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 |
1-6 of 6
1import { useEffect, useMemo, useState } from 'react';2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { data, type Person } from './makeData';4import { Button } from '@mui/material';56const Example = () => {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 |
---|---|---|---|---|
0-0 of 0
1import { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 type MRT_ColumnDef,5 type MRT_ColumnFiltersState,6 type MRT_PaginationState,7 type MRT_SortingState,8} from 'material-react-table';9import { IconButton, Tooltip } from '@mui/material';10import RefreshIcon from '@mui/icons-material/Refresh';11import {12 QueryClient,13 QueryClientProvider,14 keepPreviousData,15 useQuery,16} from '@tanstack/react-query';1718type UserApiResponse = {19 data: Array<User>;20 meta: {21 totalRowCount: number;22 };23};2425type User = {26 firstName: string;27 lastName: string;28 address: string;29 state: string;30 phoneNumber: string;31};3233const Example = () => {34 const [columnFilters, setColumnFilters] = useState<MRT_ColumnFiltersState>(35 [],36 );37 const [globalFilter, setGlobalFilter] = useState('');38 const [sorting, setSorting] = useState<MRT_SortingState>([]);39 const [pagination, setPagination] = useState<MRT_PaginationState>({40 pageIndex: 0,41 pageSize: 10,42 });4344 const { data, isError, isRefetching, isLoading, refetch } =45 useQuery<UserApiResponse>({46 queryKey: [47 'table-data',48 columnFilters, //refetch when columnFilters changes49 globalFilter, //refetch when globalFilter changes50 pagination.pageIndex, //refetch when pagination.pageIndex changes51 pagination.pageSize, //refetch when pagination.pageSize changes52 sorting, //refetch when sorting changes53 ],54 queryFn: async () => {55 const fetchURL = new URL(56 '/api/data',57 process.env.NODE_ENV === 'production'58 ? 'https://www.material-react-table.com'59 : 'http://localhost:3000',60 );61 fetchURL.searchParams.set(62 'start',63 `${pagination.pageIndex * pagination.pageSize}`,64 );65 fetchURL.searchParams.set('size', `${pagination.pageSize}`);66 fetchURL.searchParams.set(67 'filters',68 JSON.stringify(columnFilters ?? []),69 );70 fetchURL.searchParams.set('globalFilter', globalFilter ?? '');71 fetchURL.searchParams.set('sorting', JSON.stringify(sorting ?? []));7273 const response = await fetch(fetchURL.href);74 const json = (await response.json()) as UserApiResponse;75 return json;76 },77 placeholderData: keepPreviousData,78 });7980 const columns = useMemo<MRT_ColumnDef<User>[]>(81 () => [82 {83 accessorKey: 'firstName',84 header: 'First Name',85 },86 {87 accessorKey: 'lastName',88 header: 'Last Name',89 },90 {91 accessorKey: 'address',92 header: 'Address',93 },94 {95 accessorKey: 'state',96 header: 'State',97 },98 {99 accessorKey: 'phoneNumber',100 header: 'Phone Number',101 },102 ],103 [],104 );105106 return (107 <MaterialReactTable108 columns={columns}109 data={data?.data ?? []} //data is undefined on first render110 initialState={{ showColumnFilters: true }}111 manualFiltering112 manualPagination113 manualSorting114 muiToolbarAlertBannerProps={115 isError116 ? {117 color: 'error',118 children: 'Error loading data',119 }120 : undefined121 }122 onColumnFiltersChange={setColumnFilters}123 onGlobalFilterChange={setGlobalFilter}124 onPaginationChange={setPagination}125 onSortingChange={setSorting}126 renderTopToolbarCustomActions={() => (127 <Tooltip arrow title="Refresh Data">128 <IconButton onClick={() => refetch()}>129 <RefreshIcon />130 </IconButton>131 </Tooltip>132 )}133 rowCount={data?.meta?.totalRowCount ?? 0}134 state={{135 columnFilters,136 globalFilter,137 isLoading,138 pagination,139 showAlertBanner: isError,140 showProgressBars: isRefetching,141 sorting,142 }}143 />144 );145};146147const queryClient = new QueryClient();148149const ExampleWithReactQueryProvider = () => (150 //App.tsx or AppProviders file151 <QueryClientProvider client={queryClient}>152 <Example />153 </QueryClientProvider>154);155156export default ExampleWithReactQueryProvider;157