React Table
refine offers a TanStack Table adapter with @refinedev/react-table that allows you to use the TanStack Table library with refine. All features such as sorting, filtering, and pagination come out of the box. Under the hood it uses useList
for the fetch. Since it is designed as headless, It expects you to handle the UI.
All of TanStack Table's features are supported and you can use all of the TanStack Table's examples with no changes just copy and paste them into your project.
useTable
hook is extended from useTable
hook from the @refinedev/core
package. This means that you can use all the features of useTable
hook.
Installationβ
Install the @refinedev/react-table
library.
- npm
- yarn
- pnpm
npm i @refinedev/react-table
yarn add @refinedev/react-table
pnpm add @refinedev/react-table
Basic Usageβ
In basic usage, useTable
returns the data as it comes from the endpoint. By default, it reads resource
from the url.
Paginationβ
TanStack Table provides a bunch of methods that we can use to control the pagination. For example, we can use the setPageSize
method to set the current pageSize
. Every change in the pageSize
and pageIndex
will trigger a new request to the data provider.
It also syncs the pagination state with the URL if you enable the syncWithLocation
.
By default, pagination happens on the server side. If you want to do pagination handling on the client side, you can pass the pagination.mode property and set it to "client". Also, you can disable the pagination by setting the "off".
Sortingβ
TanStack Table provides a bunch of methods that we can use to control the sorting. For example, we can use the setColumnOrder
method to set the current sorting
value. Every change in the sorting
state will trigger a new request to the data provider.
It also syncs the sorting state with the URL if you enable the syncWithLocation
.
Filteringβ
TanStack Table provides a bunch of methods that we can use to control the filtering. For example, we can use the setColumnFilters
method to set the current columnFilters
value. Every change in the filter
will trigger a new request to the data provider.
It also syncs the filtering state with the URL if you enable the syncWithLocation
.
By default, filter operators are set to "eq" for all fields. You can specify which field will be filtered with which filter operator with the filterOperator
property in the meta
object. Just be aware that the filterOperator
must be a CrudOperators
type.
Realtime Updatesβ
This feature is only available if you use a Live Provider.
When the useTable
hook is mounted, it will call the subscribe
method from the liveProvider
with some parameters such as channel
, resource
etc. It is useful when you want to subscribe to live updates.
Refer to the liveProvider
documentation for more information β
Propertiesβ
It also accepts all props of TanStack Table.
resource
β
Default: It reads the
resource
value from the current URL.
It will be passed to the getList
method from the dataProvider
as parameter via the useList
hook.
The parameter is usually used as an API endpoint path.
It all depends on how to handle the resource
in the getList
method.
See the creating a data provider section for an example of how resources are handled.
useTable({
refineCoreProps: {
resource: "categories",
},
});
If you have multiple resources with the same name, you can pass the identifier
instead of the name
of the resource. It will only be used as the main matching key for the resource, data provider methods will still work with the name
of the resource defined in the <Refine/>
component.
For more information, refer to the
identifier
of the<Refine/>
component documentation β
dataProviderName
β
If there is more than one dataProvider
, you should use the dataProviderName
that you will use. It is useful when you want to use a different dataProvider
for a specific resource.
useTable({
refineCoreProps: {
dataProviderName: "second-data-provider",
},
});
pagination.current
β
Default:
1
Sets the initial value of the page index.
useTable({
refineCoreProps: {
pagination: {
current: 2,
},
},
});
pagination.pageSize
β
Default:
10
Sets the initial value of the page size.
useTable({
refineCoreProps: {
pagination: {
pageSize: 10,
},
},
});
pagination.mode
β
Default:
"server"
It can be "off"
, "server"
or "client"
.
- "off": Pagination is disabled. All records will be fetched.
- "client": Pagination is done on the client side. All records will be fetched and then the records will be paginated on the client side.
- "server":: Pagination is done on the server side. Records will be fetched by using the
current
andpageSize
values.
useTable({
refineCoreProps: {
pagination: {
mode: "client",
},
},
});
sorters.initial
β
Sets the initial value of the sorter. The initial
is not permanent. It will be cleared when the user changes the sorter. If you want to set a permanent value, use the sorters.permanent
prop.
Refer to the CrudSorting
interface for more information β
useTable({
refineCoreProps: {
sorters: {
initial: [
{
field: "name",
order: "asc",
},
],
},
},
});
sorters.permanent
β
Sets the permanent value of the sorter. The permanent
is permanent and unchangeable. It will not be cleared when the user changes the sorter. If you want to set a temporary value, use the sorters.initial
prop.
Refer to the CrudSorting
interface for more information β
useTable({
refineCoreProps: {
sorters: {
permanent: [
{
field: "name",
order: "asc",
},
],
},
},
});
sorters.mode
β
Default:
"server"
It can be "off"
or "server"
.
- "off": Sorting are disabled. All records will be fetched.
- "server":: Sorting are done on the server side. Records will be fetched by using the
sorters
value.
useTable({
refineCoreProps: {
sorters: {
mode: "off",
},
},
});
filters.initial
β
Sets the initial value of the filter. The initial
is not permanent. It will be cleared when the user changes the filter. If you want to set a permanent value, use the filters.permanent
prop.
Refer to the CrudFilters
interface for more information β
useTable({
refineCoreProps: {
filters: {
initial: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
},
});
filters.permanent
β
Sets the permanent value of the filter. The permanent
is permanent and unchangeable. It will not be cleared when the user changes the filter. If you want to set a temporary value, use the filters.initial
prop.
Refer to the CrudFilters
interface for more information β
useTable({
refineCoreProps: {
filters: {
permanent: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
},
});
filters.defaultBehavior
β
Default:
replace
The filtering behavior can be set to either "merge"
or "replace"
.
When the filter behavior is set to
"merge"
, it will merge the new filter with the existing filters. This means that if the new filter has the same column as an existing filter, the new filter will replace the existing filter for that column. If the new filter has a different column than the existing filters, it will be added to the existing filters.When the filter behavior is set to
"replace"
, it will replace all existing filters with the new filter. This means that any existing filters will be removed and only the new filter will be applied to the table.
You can also override the default value by using the second parameter of the setFilters
function.
useTable({
refineCoreProps: {
filters: {
defaultBehavior: "merge",
},
},
});
filters.mode
β
Default:
"server"
It can be "off"
or "server"
.
- "off":
filters
are not sent to the server. You can use thefilters
value to filter the records on the client side. - "server":: Filters are done on the server side. Records will be fetched by using the
filters
value.
useTable({
refineCoreProps: {
filters: {
mode: "off",
},
},
});
syncWithLocation
β
Default:
false
When you use the syncWithLocation feature, the useTable
's state (e.g. sort order, filters, pagination) is automatically encoded in the query parameters of the URL, and when the URL changes, the useTable
state is automatically updated to match. This makes it easy to share table state across different routes or pages and to allow users to bookmark or share links to specific table views.
Also you can set this value globally on <Refine>
component.
useTable({
refineCoreProps: {
syncWithLocation: true,
},
});
queryOptions
β
useTable
uses useList
hook to fetch data. You can pass queryOptions
.
useTable({
refineCoreProps: {
queryOptions: {
retry: 3,
},
},
});
meta
β
meta
is a special property that can be used to pass additional information to data provider methods for the following purposes:
- Customizing the data provider methods for specific use cases.
- Generating GraphQL queries using plain JavaScript Objects (JSON).
Refer to the meta
section of the General Concepts documentation for more information β
In the following example, we pass the headers
property in the meta
object to the create
method. With similar logic, you can pass any properties to specifically handle the data provider methods.
useTable({
refineCoreProps: {
metaData: {
headers: { "x-meta-data": "true" },
},
},
});
const myDataProvider = {
//...
getList: async ({
resource,
pagination,
sorters,
filters,
metaData,
}) => {
const headers = metaData?.headers ?? {};
const url = `${apiUrl}/${resource}`;
//...
//...
const { data, headers } = await httpClient.get(`${url}`, { headers });
return {
data,
};
},
//...
};
successNotification
β
NotificationProvider
is required for this prop to work.
After data is fetched successfully, useTable
can call open
function from NotificationProvider
to show a success notification. With this prop, you can customize the success notification.
useTable({
refineCoreProps: {
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
},
});
errorNotification
β
NotificationProvider
is required for this prop to work.
After data fetching is failed, useTable
will call open
function from NotificationProvider
to show an error notification. With this prop, you can customize the error notification.
useTable({
refineCoreProps: {
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
},
});
liveMode
β
LiveProvider
is required.
Determines whether to update data automatically ("auto") or not ("manual") if a related live event is received. It can be used to update and show data in Realtime throughout your app. For more information about live mode, please check Live / Realtime page.
useTable({
refineCoreProps: {
liveMode: "auto",
},
});
onLiveEvent
β
LiveProvider
is required.
The callback function is executed when new events from a subscription have arrived.
useTable({
refineCoreProps: {
onLiveEvent: (event) => {
console.log(event);
},
},
});
liveParams
β
LiveProvider
is required.
Params to pass to liveProvider's subscribe method.
initialCurrent
β
initialCurrent
Use pagination.current
instead.
Default:
1
Sets the initial value of the page index.
useTable({
refineCoreProps: {
initialCurrent: 2,
},
});
initialPageSize
β
initialPageSize
Use pagination.pageSize
instead.
Default:
10
Sets the initial value of the page size.
useTable({
refineCoreProps: {
initialPageSize: 20,
},
});
hasPagination
β
hasPagination
Use pagination.mode
instead.
Default:
true
Determines whether to use server-side pagination or not.
useTable({
refineCoreProps: {
hasPagination: false,
},
});
initialSorter
β
initialSorter
Use sorters.initial
instead.
Sets the initial value of the sorter. The initialSorter
is not permanent. It will be cleared when the user changes the sorter. If you want to set a permanent value, use the permanentSorter
prop.
Refer to the CrudSorting
interface for more information β
useTable({
refineCoreProps: {
initialSorter: [
{
field: "name",
order: "asc",
},
],
},
});
permanentSorter
β
permanentSorter
Use sorters.permanent
instead.
Sets the permanent value of the sorter. The permanentSorter
is permanent and unchangeable. It will not be cleared when the user changes the sorter. If you want to set a temporary value, use the initialSorter
prop.
Refer to the CrudSorting
interface for more information β
useTable({
refineCoreProps: {
permanentSorter: [
{
field: "name",
order: "asc",
},
],
},
});
initialFilter
β
initialFilter
Use filters.initial
instead.
Sets the initial value of the filter. The initialFilter
is not permanent. It will be cleared when the user changes the filter. If you want to set a permanent value, use the permanentFilter
prop.
Refer to the CrudFilters
interface for more information β
useTable({
refineCoreProps: {
initialFilter: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
});
permanentFilter
β
permanentFilter
Use filters.permanent
instead.
Sets the permanent value of the filter. The permanentFilter
is permanent and unchangeable. It will not be cleared when the user changes the filter. If you want to set a temporary value, use the initialFilter
prop.
Refer to the CrudFilters
interface for more information β
useTable({
refineCoreProps: {
permanentFilter: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
});
defaultSetFilterBehavior
β
defaultSetFilterBehavior
Use filters.defaultBehavior
instead.
Default:
replace
The filtering behavior can be set to either "merge"
or "replace"
.
When the filter behavior is set to
"merge"
, it will merge the new filter with the existing filters. This means that if the new filter has the same column as an existing filter, the new filter will replace the existing filter for that column. If the new filter has a different column than the existing filters, it will be added to the existing filters.When the filter behavior is set to
"replace"
, it will replace all existing filters with the new filter. This means that any existing filters will be removed and only the new filter will be applied to the table.
You can also override the default value by using the second parameter of the setFilters
function.
useTable({
refineCoreProps: {
defaultSetFilterBehavior: "merge",
},
});
Return Valuesβ
It also have all return values of TanStack Table.
refineCore
β
tableQueryResult
β
Returned values from useList
hook.
sorters
β
Current sorters state.
setSorters
β
A function to set current sorters state.
(sorters: CrudSorting) => void;
A function to set current sorters state.
filters
β
Current filters state.
setFilters
β
((filters: CrudFilters, behavior?: SetFilterBehavior) => void) & ((setter: (prevFilters: CrudFilters) => CrudFilters) => void)
A function to set current filters state.
current
β
Current page index state. If pagination is disabled, it will be undefined
.
setCurrent
β
React.Dispatch<React.SetStateAction<number>> | undefined;
A function to set the current page index state. If pagination is disabled, it will be undefined
.
pageSize
β
Current page size state. If pagination is disabled, it will be undefined
.
setPageSize
β
React.Dispatch<React.SetStateAction<number>> | undefined;
A function to set the current page size state. If pagination is disabled, it will be undefined
.
pageCount
β
Total page count state. If pagination is disabled, it will be undefined
.
createLinkForSyncWithLocation
β
(params: SyncWithLocationParams) => string;
A function creates accessible links for syncWithLocation
. It takes SyncWithLocationParams as parameters.
sorter
β
sorter
Use sorters
instead.
Current sorters state.
setSorter
β
setSorter
Use setSorters
instead.
A function to set current sorters state.
(sorters: CrudSorting) => void;
FAQβ
How can I handle relational data?β
You can use useMany
hook to fetch relational data.
How can I handle client side filtering?β
You can set the filters.mode: "off"
in order to disable server-side filtering. useTable
is fully compatible with TanStack Table
filtering feature.
useTable({
refineCoreProps: {
filters: {
mode: "off",
},
},
});
How can I handle client side sorting?β
You can set the sorters.mode: "off"
in order to disable server-side sorting. useTable
is fully compatible with TanStack Table
sorting feature.
useTable({
refineCoreProps: {
sorters: {
mode: "off",
},
},
});
API Referenceβ
Propertiesβ
Type Parametersβ
Property | Desription | Type | Default |
---|---|---|---|
TQueryFnData | Result data of the query. Extends BaseRecord | BaseRecord | BaseRecord |
TError | Custom error object that extends HttpError | HttpError | HttpError |
TData | Result data of the select function. Extends BaseRecord . If not specified, the value of TQueryFnData will be used as the default value. | BaseRecord | TQueryFnData |
Return valuesβ
Property | Description | Type |
---|---|---|
refineCore | The return values of the useTable in the core | UseTableReturnValues |
Tanstack Table Return Values | See TanStack Table documentation |
Exampleβ
npm create refine-app@latest -- --example table-react-table-basic
- Installation
- Basic Usage
- Pagination
- Sorting
- Filtering
- Realtime Updates
- Properties
resource
dataProviderName
pagination.current
pagination.pageSize
pagination.mode
sorters.initial
sorters.permanent
sorters.mode
filters.initial
filters.permanent
filters.defaultBehavior
filters.mode
syncWithLocation
queryOptions
meta
successNotification
errorNotification
liveMode
onLiveEvent
liveParams
initialCurrent
initialPageSize
hasPagination
initialSorter
permanentSorter
initialFilter
permanentFilter
defaultSetFilterBehavior
- Return Values
refineCore
tableQueryResult
sorters
setSorters
filters
setFilters
current
setCurrent
pageSize
setPageSize
pageCount
createLinkForSyncWithLocation
sorter
setSorter
- FAQ
- How can I handle relational data?
- How can I handle client side filtering?
- How can I handle client side sorting?
- API Reference
- Properties
- Type Parameters
- Return values
- Example