Remote Data Example
You will most likely be using a remote data source for your table, which is fully supported. Here is an example of data being fetched from a remote server but also filtered, paginated, and sorted on the server.
Also, be sure to check out the TanStack React Query Example, which is very similar to this one, except it uses react-query to simplify much of the state management needed for fetching data.
First Name | Last Name | Address | State | Phone Number | |
---|---|---|---|---|---|
No records to display |
1import React, { FC, useEffect, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import type {4 ColumnFiltersState,5 PaginationState,6 SortingState,7} from '@tanstack/react-table';89type UserApiResponse = {10 data: Array<User>;11 meta: {12 totalRowCount: number;13 };14};1516type User = {17 firstName: string;18 lastName: string;19 address: string;20 state: string;21 phoneNumber: string;22};2324const Example: FC = () => {25 //data and fetching state26 const [data, setData] = useState<User[]>([]);27 const [isError, setIsError] = useState(false);28 const [isLoading, setIsLoading] = useState(false);29 const [isRefetching, setIsRefetching] = useState(false);30 const [rowCount, setRowCount] = useState(0);3132 //table state33 const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);34 const [globalFilter, setGlobalFilter] = useState('');35 const [sorting, setSorting] = useState<SortingState>([]);36 const [pagination, setPagination] = useState<PaginationState>({37 pageIndex: 0,38 pageSize: 10,39 });4041 //if you want to avoid useEffect, look at the React Query example instead42 useEffect(() => {43 const fetchData = async () => {44 if (!data.length) {45 setIsLoading(true);46 } else {47 setIsRefetching(true);48 }4950 const url = new URL(51 '/api/data',52 process.env.NODE_ENV === 'production'53 ? 'https://www.material-react-table.com'54 : 'http://localhost:3000',55 );56 url.searchParams.set(57 'start',58 `${pagination.pageIndex * pagination.pageSize}`,59 );60 url.searchParams.set('size', `${pagination.pageSize}`);61 url.searchParams.set('filters', JSON.stringify(columnFilters ?? []));62 url.searchParams.set('globalFilter', globalFilter ?? '');63 url.searchParams.set('sorting', JSON.stringify(sorting ?? []));6465 try {66 const response = await fetch(url.href);67 const json = (await response.json()) as UserApiResponse;68 setData(json.data);69 setRowCount(json.meta.totalRowCount);70 } catch (error) {71 setIsError(true);72 console.error(error);73 return;74 }75 setIsError(false);76 setIsLoading(false);77 setIsRefetching(false);78 };79 fetchData();80 // eslint-disable-next-line react-hooks/exhaustive-deps81 }, [82 columnFilters,83 globalFilter,84 pagination.pageIndex,85 pagination.pageSize,86 sorting,87 ]);8889 const columns = useMemo<MRT_ColumnDef<User>[]>(90 () => [91 {92 accessorKey: 'firstName',93 header: 'First Name',94 },95 //column definitions...113 ],114 [],115 );116117 return (118 <MaterialReactTable119 columns={columns}120 data={data}121 enableRowSelection122 getRowId={(row) => row.phoneNumber}123 initialState={{ showColumnFilters: true }}124 manualFiltering125 manualPagination126 manualSorting127 muiToolbarAlertBannerProps={128 isError129 ? {130 color: 'error',131 children: 'Error loading data',132 }133 : undefined134 }135 onColumnFiltersChange={setColumnFilters}136 onGlobalFilterChange={setGlobalFilter}137 onPaginationChange={setPagination}138 onSortingChange={setSorting}139 rowCount={rowCount}140 state={{141 columnFilters,142 globalFilter,143 isLoading,144 pagination,145 showAlertBanner: isError,146 showProgressBars: isRefetching,147 sorting,148 }}149 />150 );151};152153export default Example;154
View Extra Storybook Examples