How to deal with a resource name that has a variable in it?
My resource name is something like
categories/:id/posts
this is a list of posts for a specific categories. How can I create a resources based on this url name convention?11 Replies
Hi @dipbazz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In refine, you can define your resources and their action paths in the
<Refine>
component under the resources property. This informs refine to use these paths when generating the breadcrumbs, menus, handling redirections, and inferring the current resource and action.
For your case, you can define your resource as follows:
In the path definitions, :id
is a placeholder for the actual id value. You can use the same :id
notifier multiple times within the same route.
Remember to assign a <Route />
for each path and an element to display at that path. The path specified in the resource definition should match the path
specified in the route definition.
Here is an example:
For more information, refer to the refine documentation (https://refine.dev/docs/tutorial/understanding-resources/index/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyanOP•12mo ago
@kapa.ai when I do this in my list page the query triggred will be on
posts
url but I want to trigger the url as categories/2/posts
Let me check the knowledge base for you. One moment! :books:
----------------------
To achieve this, you can create a new resource using the
identity
key to differentiate two URLs in Refine
. Based on the params of the category, you can set filters in a useTable
hook. Here's an example:
In your post page component's useTable
hook, you can set the filters based on the categoryId
as follows:
This solution was discussed in a Discord support thread (https://discord.com/channels/837692625737613362/1153268482620796948) and was confirmed as a good approach by the support staff.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyanOP•12mo ago
@kapa.ai I have a url as
categories/:id/posts
and I don't have access to posts
url.Let me check the knowledge base for you. One moment! :books:
----------------------
In refine, you can define your resources and their action paths in the
<Refine>
component under the resources property. This informs refine to use these paths when generating the breadcrumbs, menus, handling redirections, and inferring the current resource and action.
For your case, you can define your resource as follows:
In the path definitions, :id
is a placeholder for the actual id value. You can use the same :id
notifier multiple times within the same route.
Remember to assign a <Route />
for each path and an element to display at that path. The path specified in the resource definition should match the path
specified in the route definition.
Here is an example:
For more information, refer to the refine documentation (https://refine.dev/docs/tutorial/understanding-resources/index/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyanOP•12mo ago
@kapa.ai, if I use my resource name as
categories/:id/:posts
my list page will hit an api endpoint with categories/:id/posts
where the endpoint should be as categories/2/posts
Let me check the knowledge base for you. One moment! :books:
----------------------
In refine, you can use the
resource
property on all data hooks to make a request to an API with a nested route. The resource
property can be thought of as the URL.
For example, if you want to make a request to the URL /categories/2/posts
, you can use the useTable
hook as follows:
This will make a request to the URL /categories/2/posts
.
However, if you want to make this dynamic based on the id
, you would need to pass the id
dynamically to the resource
property. This can be done by getting the id
from the route parameters or from the state.
Here is an example of how you can do this:
In this example, useParams
is a hook from react-router-dom
that returns the dynamic parts of the route.
For more information, refer to the refine documentation (https://refine.dev/docs/guides-concepts/faq#how-can-i-request-an-api-with-nested-route).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below