getToPath does not give correct url

I use
getToPath({
resource: select("myresource").resource,
action: "show",
meta: {
id: myId,
},
})
getToPath({
resource: select("myresource").resource,
action: "show",
meta: {
id: myId,
},
})
with a route
<Route path="/exports">
<Route index element={<MyResourceList></MyResourceList>} />
<Route path="show/:id" element={<MyResourceShow/>} />
<Route path="create" element={<CreateResource/>} />
</Route>
<Route path="/exports">
<Route index element={<MyResourceList></MyResourceList>} />
<Route path="show/:id" element={<MyResourceShow/>} />
<Route path="create" element={<CreateResource/>} />
</Route>
it will return /myresource/myId not /myresourrce/show/myId as I thought it would Also example of "useGetToPath" online is broken, the resource parameter is not a string but an IResourceItem
2 Replies
kapa.ai
kapa.ai4w 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: ---------------------- Based on the information provided in the knowledge sources, I can address your question about the getToPath function and the issues you're experiencing. 1. Regarding the incorrect URL: The behavior you're experiencing with getToPath not returning the expected URL is likely due to how your resources are defined. The getToPath function relies on the resource definitions to construct the URL. Make sure your resource is properly defined with the correct show action path. For example, in your <Refine> component, you should have a resource definition like this:
resources={[
{
name: "myresource",
list: "/exports",
show: "/exports/show/:id",
}
]}
resources={[
{
name: "myresource",
list: "/exports",
show: "/exports/show/:id",
}
]}
This definition should match your route structure Relationship Between Resources and Routes (https://refine.dev/docs/guides-concepts/routing/#relationship-between-resources-and-routes-). 2. Regarding the useGetToPath documentation: You're correct that there seems to be an issue with the documentation for useGetToPath. The resource parameter should indeed be an IResourceItem object, not a string. This is likely a documentation error useGetToPath Documentation (https://refine.dev/docs/routing/hooks/use-get-to-path/). To use useGetToPath correctly, you should do something like this:
import { useGetToPath, useResource } from "@refinedev/core";

const { select } = useResource();
const getToPath = useGetToPath();

const onClick = () => {
const to = getToPath({
resource: select("myresource"),
action: "show",
meta: { id: myId },
});
// Use the 'to' value for navigation
}
import { useGetToPath, useResource } from "@refinedev/core";

const { select } = useResource();
const getToPath = useGetToPath();

const onClick = () => {
const to = getToPath({
resource: select("myresource"),
action: "show",
meta: { id: myId },
});
// Use the 'to' value for navigation
}
Note that select("myresource") returns the resource object, which is what getToPath expects Hook Integrations (https://refine.dev/docs/guides-concepts/routing/#hook-integrations). If you're still experiencing issues after making these adjustments, it might be helpful to double-check your resource definitions and routing setup. If the problem persists, you may want to consider reporting this as a potential bug to the refine team. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w 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?