Add Support for Named Routes in TanStack Router #3847
krishantalwar
started this conversation in
Ideas
Replies: 1 comment
-
we had this already here : #2456 please rather add comments to existing ones if they match closely |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary:
I would like to suggest adding named routes functionality to TanStack Router, similar to what is available in other popular routing libraries like Laravel. Named routes allow developers to reference routes by a human-readable name instead of hardcoding the full path, which makes the codebase more maintainable and flexible, especially for larger applications.
Motivation:
Currently, TanStack Router is a powerful and flexible routing solution, but in many cases, it would be beneficial to reference routes by name instead of repeating the path in multiple places. This is especially useful in scenarios such as:
Proposed Solution:
Here's how I envision the named routes feature working in TanStack Router:
Define Routes with a
name
Property:Allow routes to be defined with a
name
property for human-readable identification.Example:
Generate URL from Route Name:
Provide a helper function (e.g.,
generatePath
) that takes the route name and parameters and returns the corresponding URL path.Example:
Linking by Route Name:
Allow
<Link />
components to accept the route name, and automatically resolve the correct URL.Example:
Redirecting by Route Name:
Allow redirection by route name instead of using the path directly.
Example:
Handle Dynamic Parameters:
Ensure dynamic parameters in route paths (like
/user/:id
) work seamlessly with named routes.Example:
Benefits of Named Routes:
Maintainability: If the path for a route changes, you only need to update the route definition, rather than all occurrences of the URL in your code.
Readability: Using meaningful route names makes your code more semantic, improving developer experience.
Flexibility: Named routes can be used in places like redirects, links, or even for programmatically generating URLs.
Example Use Case:
In a large application, having named routes means that if the URL structure changes (e.g., from
/user/:id
to/profile/:userId
), developers don’t need to find and update every instance of that path across the codebase. Instead, they just need to change the route definition.Example:
Next Steps:
I would love to hear feedback on this feature request. Is this something that could be useful for TanStack Router? I'd be happy to help with brainstorming or prototyping if needed!
Beta Was this translation helpful? Give feedback.
All reactions