Skip to main content
Version: 4.xx.xxSource Code

useCustom

useCustom is an extended version of TanStack Query's useQuery. It supports all the features of useQuery and adds some extra features.

  • It uses the custom method as the query function from the dataProvider which is passed to <Refine>.

It is useful when you want to send a custom query request using the TanStack Query advantages.

attention

useCustom should not be used when creating, updating, or deleting a resource. To do these; useCreate, useUpdate or useDelete hooks should be used instead.

This is because useCustom, unlike other data hooks, does not invalidate queries and therefore will not update the application state either.

If you need to custom mutation request, use the useCustomMutation hook.

Basic Usage

The useCustom hook expects a url and method properties. These parameters will be passed to the custom method from the dataProvider as a parameter.

When properties are changed, the useCustom hook will trigger a new request.

import { useCustom, useApiUrl } from "@refinedev/core";

interface PostUniqueCheckResponse {
isAvailable: boolean;
}

const apiUrl = useApiUrl();

const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});

Properties

url
required

It will be passed to the custom method from the dataProvider as a parameter. It is usually used to specify the endpoint of the request.

useCustom({
url: "www.example.com/api/get-products",
});

method
required

It will be passed to the custom method from the dataProvider as a parameter. It is usually used to specify the HTTP method of the request.

useCustom({
method: "get",
});

config.headers

It will be passed to the custom method from the dataProvider as a parameter. It can be used to specify the headers of the request.

useCustom({
config: {
headers: {
"x-custom-header": "foo-bar",
},
},
});

config.query

It will be passed to the custom method from the dataProvider as a parameter. It can be used to specify the query parameters of the request.

useCustom({
config: {
query: {
title: "Foo bar",
},
},
});

config.payload

It will be passed to the custom method from the dataProvider as a parameter. It can be used to specify the payload of the request.

useCustom({
config: {
payload: {
title: "Foo bar",
},
},
});

config.sorters

It will be passed to the custom method from the dataProvider as a parameter. It can be used to send the sort query parameters of the request.

useCustom({
config: {
sorters: [
{
field: "title",
order: "asc",
},
],
},
});

config.filters

It will be passed to the custom method from the dataProvider as a parameter. It can be used to send the filter query parameters of the request.

useCustom({
config: {
filters: [
{
field: "title",
operator: "contains",
value: "Foo",
},
],
},
});

config.sort

Deprecated

Use config.sorters instead.

queryOptions

queryOptions is used to pass additional options to the useQuery hook. It is useful when you want to pass additional options to the useQuery hook.

Refer to the useQuery documentation for more information &#8594

useCustom({
queryOptions: {
retry: 3,
enabled: false,
},
});

meta

meta is used following two purposes:

  • To pass additional information to data provider methods.
  • Generate GraphQL queries using plain JavaScript Objects (JSON). Please refer GraphQL for more information.

In the following example, meta is passed to the custom method from the dataProvider as a parameter.

useCustom({
meta: {
foo: "bar",
},
});

const myDataProvider = {
//...
custom: async ({
url,
method,
sort,
filters,
payload,
query,
headers,
meta,
}) => {
const foo = meta?.foo;

console.log(foo); // "bar"

//...
},
//...
};

dataProviderName

If there is more than one dataProvider, you can specify which one to use by passing the dataProviderName prop. It is useful when you have a different data provider for different resources.

useCustom({
dataProviderName: "second-data-provider",
});

successNotification

NotificationProvider is required for this prop to work.

After data is fetched successfully, useCustom can call open function from NotificationProvider to show a success notification. With this prop, you can customize the success notification.

useCustom({
successNotification: (data, values) => {
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, useCustom will call open function from NotificationProvider to show an error notification. With this prop, you can customize the error notification.

useCustom({
errorNotification: (data, values) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});

FAQ

How to invalidate the custom query?

To invalidate a query, you can use the invalidateQueries method from the useQueryClient hook provided by the @tanstack/react-query library.

import { useQueryClient } from "@tanstack/react-query";

const queryClient = useQueryClient();

queryClient.invalidateQueries(["custom-key"]);

Note that you'll need to know the query key to invalidate the query. If you don't know the query key, you can use the queryOptions property of the useCustom hook.

import { useCustom } from "@refinedev/core";

useCustom({
queryOptions: {
queryKey: ["custom-key"],
},
});
tip

By default, the query key is generated based on the properties passed to useCustom hook, you can see it in the @tanstack/react-query devtools panel.

API

Properties

Type Parameters

PropertyDesriptionTypeDefault
TQueryFnDataResult data returned by the query function. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TQueryValues for query params.TQueryunknown
TPayloadValues for params.TPayloadunknown
TDataResult data returned by the select function. Extends BaseRecord. If not specified, the value of TQueryFnData will be used as the default value.BaseRecordTQueryFnData

Return value

DescriptionType
Result of the TanStack Query's useQueryQueryObserverResult<CustomResponse<TData>, TError>