Skip to main content
Version: 2.xx.xx

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 to false
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

PropertyDescriptionTypeDefault
resource
Required
Resource name for API data interactionsstring
ids
Required
ids of the item in the resourcestring[]
queryOptionsreact-query's useQuery options UseQueryOptions<
{ data: TData[]; },
TError>
successNotificationSuccessful Query notificationSuccessErrorNotificationfalse
errorNotificationUnsuccessful Query notificationSuccessErrorNotification"Error (status code: statusCode)"
metaDataMetadata query for dataProviderMetaDataQuery{}
liveModeWhether 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"
liveParamsParams to pass to liveProvider's subscribe method if liveMode is enabled.{ ids?: string[]; [key: string]: any; }undefined
onLiveEventCallback to handle all related live events of this hook.(event: LiveEvent) => voidundefined

Type Parameters

PropertyDesriptionTypeDefault
TDataResult data of the query. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError

Return values

DescriptionType
Result of the react-query's useQueryQueryObserverResult<{ data: TData[]; }>