wise-white
wise-white3mo ago

Create a custom hook like useMany or useOne without sending ids

I have this
const { data } = useMany<any, HttpError>({
ids: [],
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
const { data } = useMany<any, HttpError>({
ids: [],
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
And this
const { data } = useOne<any, HttpError>({
id: 0,
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
const { data } = useOne<any, HttpError>({
id: 0,
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
But I want to have something like
const { data } = useGet<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
const { data } = useGet<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
To avoid sendig and empty array of ids or an invalid id.
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @Sebastos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the extracts, there isn't a direct way to create a custom hook like useMany or useOne without sending ids in Refine. The useMany and useOne hooks are designed to fetch multiple records and a single record respectively, based on the ids provided. However, if you want to fetch data from a resource without specifying ids, you can use the useList or useInfiniteList hooks. These hooks are designed to fetch a list of records from a resource. Here's an example:
const { data } = useList<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
const { data } = useList<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
});
If you need to make custom API requests, you can use the useCustom hook. This hook allows you to make custom requests to your API. Here's an example:
const { data } = useCustom<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
custom: ({ resource }) => ({
url: `${resource}`,
method: "GET",
}),
});
const { data } = useCustom<any, HttpError>({
resource: RESOURCES.WEB_FORM_LOCATIONS,
custom: ({ resource }) => ({
url: `${resource}`,
method: "GET",
}),
});
Please note that the useCustom hook requires a custom function that returns an object with url and method properties. For more information, you can refer to the Refine documentation (https://refine.dev/docs/data/hooks/use-custom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@689846866837373077> kapa.ai is still learning and improving, please let us know how it did by reacting below