rival-black
rival-black4d ago

useupdate without id

I want to incorporate the id in the resource path, or append an extra part of the url when using useupdate, how can I do that
2 Replies
kapa.ai
kapa.ai4d ago
Hi @Todilo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Refine's useUpdate hook expects the id to be provided as a separate parameter, not embedded within the resource path. When you call mutate, the id is passed to the dataProvider's update method as a separate argument, and the resource parameter is typically just the base path (e.g., "products"), not a path with :id or a custom segment. If you want to customize the URL structure (for example, to append an extra part or incorporate the id directly into the path), you need to handle this logic inside your custom dataProvider. The mutate call itself does not interpolate the id into the resource string; it simply passes both resource and id to the dataProvider, and it's up to your dataProvider to construct the final URL. For example, you might call:
mutate({
resource: "products",
id: 1,
values: { name: "New Product" },
});
mutate({
resource: "products",
id: 1,
values: { name: "New Product" },
});
And in your dataProvider's update method, you would construct the URL as needed:
update: async ({ resource, id, variables }) => {
const url = `${apiUrl}/${resource}/${id}/customize`; // custom logic here
// ...
}
update: async ({ resource, id, variables }) => {
const url = `${apiUrl}/${resource}/${id}/customize`; // custom logic here
// ...
}
If you try to pass a resource like "/products/:id/customize" and an id, Refine will not interpolate :id for you; it will treat the resource string literally and append the id at the end, which is likely not what you want. The recommended approach is to keep resource and id separate and handle any custom URL logic in your dataProvider's implementation. This is also confirmed by user discussions and issues, where the workaround is to interpolate the string yourself and pass an empty id, but this is not the standard or recommended pattern in Refine as of now see related discussion. In summary:
- Pass resource and id separately to useUpdate/mutate. - Handle any custom URL construction in your dataProvider's update method. - Refine does not natively interpolate id into resource paths. If you need to pass additional information, you can use the meta property to send custom data to your dataProvider, but this does not affect URL construction directly see meta usage. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@207945811592478720> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?