Skip to main content
Version: 3.xx.xx

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​

PropertyDescriptionTypeDefault
resource
Required
Resource name for API data interactionsstring
id
Required
id of the item in the resourceBaseKey
queryOptionsreact-query's useQuery options UseQueryOptions<
{ data: TData; },
TError>
successNotificationSuccessful Query notificationSuccessErrorNotificationfalse
errorNotificationUnsuccessful Query notificationSuccessErrorNotification"Error (status code: statusCode)"
metaDataMetadata query for dataProviderMetaDataQuery{}
dataProviderNameIf there is more than one dataProvider, you should use the dataProviderName that you will use.stringdefault
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?: BaseKey; [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; }>