handling slug

how to customise useShow to use slug field instead of an id to fetch data ?
2 Replies
kapa.ai
kapa.ai2d ago
Hi @koop_g. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To customize useShow to use a slug field instead of an ID to fetch data, you can follow these steps: 1. Pass the resource prop to useShow to specify the resource you're working with. 2. Use the id prop of useShow to pass the slug value instead of an ID. Here's an example of how you can implement this:
import { useShow, useParsed } from "@refinedev/core";

// Assuming you have the slug available from the URL or some other source
const { slug } = useParsed();

const { queryResult } = useShow({
resource: "posts",
id: slug,
});
import { useShow, useParsed } from "@refinedev/core";

// Assuming you have the slug available from the URL or some other source
const { slug } = useParsed();

const { queryResult } = useShow({
resource: "posts",
id: slug,
});
In this example, we're using the useParsed hook to get the slug from the URL, but you can obtain the slug value from wherever it's available in your application. You may also need to adjust your data provider to handle slug-based requests instead of ID-based ones. This would involve customizing the getOne method of your data provider to use the slug for fetching data. It's important to note that the useShow hook doesn't have built-in support for using fields other than id. The customization happens mainly in how you pass the identifier (in this case, the slug) to the hook and how your data provider handles it. For more information on the useShow hook and its properties, you can refer to the useShow documentation. If you need further customization, you might want to consider creating a custom hook that wraps useShow and handles the slug-to-id conversion if necessary. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2d ago
<@1327562833537400895> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?