useMany
useMany
is a modified version of react-query
's useQuery
used for retrieving multiple items from a resource
.
It uses getMany
method as query function from the dataProvider
which is passed to <Refine>
.
Usage
Let's say that we have a resource named categories
.
https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
{
id: 3,
title: "Powerful Crypto",
},
];
}
import { useMany } from "@pankod/refine";
type ICategory = {
id: string;
title: string;
};
const categoryQueryResult = useMany<ICategory>({
resource: "categories",
ids: ["1", "2"],
});
tip
useMany
can also accept all useQuery
options.
Refer to react-query docs for further information. →
- For example, to disable query from running automatically you can set
enabled
tofalse
const categoryQueryResult = useMany<ICategory>({
resource: "categories",
ids: ["1", "2"],
queryOptions: { enabled: false },
});
After query runs, the categoryQueryResult
will include the retrieved data:
categoryQueryResult.data
{
"data": [
{
"id": 1,
"title": "E-business"
},
{
"id": 2,
"title": "Virtual Invoice Avon"
}
]
}
tip
useMany
returns the result of react-query
's useQuery
which includes properties such as isLoading
and isFetching
.
Refer to react-query docs for further information. →
API
Properties
Property | Description | Type | Default |
---|---|---|---|
resource Required | Resource name for API data interactions | string | |
ids Required | ids of the item in the resource | string[] | |
queryOptions | react-query 's useQuery options | UseQueryOptions< { data: TData[]; }, TError> | |
successNotification | Successful Query notification | SuccessErrorNotification | false |
errorNotification | Unsuccessful Query notification | SuccessErrorNotification | "Error (status code: statusCode )" |
metaData | Metadata query for dataProvider | MetaDataQuery | {} |
liveMode | Whether to update data automatically ("auto" ) or not ("manual" ) if a related live event is received. The "off" value is used to avoid creating a subscription. | "auto" | "manual" | "off" | "off" |
liveParams | Params to pass to liveProvider 's subscribe method if liveMode is enabled. | { ids?: string[]; [key: string]: any; } | undefined |
onLiveEvent | Callback to handle all related live events of this hook. | (event: LiveEvent) => void | undefined |
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 |
Return values
Description | Type |
---|---|
Result of the react-query 's useQuery | QueryObserverResult<{ data: TData[]; }> |