Skip to main content

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>.

tip

If your data provider didn't implement getMany method, useMany will use getOne method multiple times instead.

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-core";

type ICategory = {
id: number;
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

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[]; }>