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,
});
提示
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
enabledtofalse
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"
}
}
提示
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
| 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; }> |