useCustom
useCustom is a modified version of react-query's useQuery used for custom requests.
It uses the custom method from the dataProvider which is passed to <Refine>.
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.
Features
- You can send a request to any link, using any of the methods (
get, delete, head, options, post, put, patch). - You can send comprehensive requests to resources with
SortandFilterparameters.
Usage
Let's make a use case. Lets say that we need to verify that the header in the post resource is unique. For this, we have an end-point similar to the one below.
https://api.fake-rest.refine.dev/posts/unique-check?title=Foo bar
{
"isAvailable": true
}
import { useCustom, useApiUrl } from "@pankod/refine-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",
},
},
});
API
Properties
Type Parameters
| Property | Desription | Type | Default |
|---|---|---|---|
| TData | Result data of the query. Extends BaseRecord | BaseRecord | BaseRecord |
| TError | Custom error object that extends HttpError | HttpError | HttpError |
| TQuery | Values for query params. | TQuery | unknown |
| TPayload | Values for params. | TPayload | unknown |
Return value
| Description | Type |
|---|---|
Result of the react-query's useQuery | QueryObserverResult<CustomResponse<TData>, TError> |