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>
.
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 have to use useCustom
for mutation operations, you can manually manage the application state with the queryResult
's refetch
and remove
methods returned from the hooks that you use.
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
Sort
andFilter
parameters.
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.
{
"isAvailable": true
}
import { useCustom, useApiUrl } from "@pankod/refine";
interface PostUniqueCheckResponse {
isAvailable: boolean;
}
const apiUrl = useApiUrl();
const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
query: {
title: "Foo bar",
},
},
});
API
Properties
Property | Description | Type |
---|---|---|
url Required | URL | string |
method Required | Method | get , delete , head , options , post , put , patch |
config | Query Params | { sort?: CrudSorting; filters?: CrudFilters ; payload?: {}; query?: {} } |
queryOptions | useQuery Options | object |
metaData | Metadata query for dataProvider | MetaDataQuery |
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> |