How to Avoid Hardcoding Paths when Navigating to other Routes
In my web application, there are many places where I need to navigate to another route. I am looking to figure out how to pull route paths from Tanstack router so I can avoid having to hardcode them throughout my application.
For instance, on the main view of a Blog page (/blog) I might have links to the following:
1. Viewing individual blog posts -- /blog/posts/$id
2. Creating new blog posts -- /blog/posts/create
3. Editing new blog posts -- /blog/posts/$id/edit
4. Deleting blog posts -- /blog/posts/$id/delete
The router is aware of all of these paths from my file based route configuration.
Is there a way from within my main (or other) pages to reach into the Router and figure out, for instance, that the path for a new blog post is /blogs/posts/create? Or the same for any other route paths configured for routes?
I found a way to do that my loading the Route from the actual file based route files, but that seems pretty dirty. I can hard code them around my application, but I had hardcoding things. What if I need to change how the paths work?
Any thoughts?
2 Replies
sensitive-blue•2mo ago
figure out, for instance, that the path for a new blog post is /blogs/posts/createfigure out how? we recommend using the string literals
exotic-emeraldOP•2mo ago
Do you mean hardcoding string literals inside when I need to use a path to another route?
Is there any reason not to load Route from a route file and access Route.fullPath? That seems to do the job nicely and avoids me hardcoding strings around the app.
Apologies if I am misunderstanding, I am still pretty new at this.