Add special compiler-replaced variable to get path to current route #4846
Replies: 4 comments 1 reply
-
Huge +1 on this. In addition to this, it would be nice if the bundler could add a // app/routes/_auth.tsx
export const useAuthLoaderData = () => {
const authMatch = useMatches().find(({ id }) => id === __ROUTE_ID__)
// const authLoaderData = useRouteLoaderData(__ROUTE_ID__)
// return authLoaderData
if (!authMatch) {
throw new Error('Unable to find auth in the url matches.')
}
return authMatch.data as SerializeFrom<typeof loader>
} |
Beta Was this translation helpful? Give feedback.
-
Is this possible yet? Would love to do this, the |
Beta Was this translation helpful? Give feedback.
-
Please accept this proposal 🙏 both suggested compiler-replaced variables would be incredibly useful! |
Beta Was this translation helpful? Give feedback.
-
FWIW, I'm coming to remix after evaluating astro, which sort of has this feature via the Astro.params global. IMO astro's implementation has a lot of weird edge case baggage like using it outside of the context of an astro layout, and I'd end up having to do a similar brittle work around as OP mentioned here. At least that was how it was when I looked last. Apart from that, I do quite like how astro allows you to access the various route segments and params directly instead of needing to parse it from a static string. But maybe that preference stems from my remix noobness. |
Beta Was this translation helpful? Give feedback.
-
Let's say I've got a full-stack component:
The
'/resources/hello'
string is problematic in this case because I could rename the file and forget to update the string.How about a "variable" that is swapped at compile time with the path to the route that is only usable within route files:
Beta Was this translation helpful? Give feedback.
All reactions