Skip to main content

useOne

useOne is a modified version of react-query's useQuery used for retrieving single items from a resource.

It uses getOne method as query function from the dataProvider which is passed to <Refine>.

Usage

Let's say that we have a resource named posts.

https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
];
}
import { useOne } from "@pankod/refine-core";

type ICategory = {
id: number;
title: string;
};

const categoryQueryResult = useOne<ICategory>({
resource: "categories",
id: 1,
});
tip

useOne can also accept all useQuery options.
Refer to react-query docs for further information.

  • For example, to disable the query from running automatically you can set enabled to false
const categoryQueryResult = useOne<ICategory>({
resource: "categories",
id: 1,
queryOptions: {
enabled: false,
},
});

After query runs, the categoryQueryResult will include the retrieved data:

categoryQueryResult.data
{
"data": {
"id": 1,
"title": "E-business"
}
}
tip

useOne returns the result of react-query's useQuery which includes many 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; }>