Code based useParams hook works with pathless id prefixed to route.
My routing tree looks like the following:
RootRoute -> Pathless_Layout_Route -> Products -> ViewProductRoute.
When i use the useParam hook like this:
It will show an error saying that the invariant is not found. Even though when i remove
useParams, the page does render.`
However when i prefix the route with the pathless layout route, it does work.
Did i miss something in the documentation? Or is this supposed to happen?14 Replies
stormy-gold•2mo ago
useParams needs a route id, not a path
so you must supply the full id (which in your case includes the layout)
genetic-orangeOP•2mo ago
Okay thanks for clearing up, but did i miss that somewhere in the documentation?
genetic-orangeOP•2mo ago
useParams hook | TanStack Router React Docs
The useParams method returns all of the path parameters that were parsed for the closest match and all of its parent matches. useParams options The useParams hook accepts an optional options object. o...
genetic-orangeOP•2mo ago
Because i was not able to find that out on this page.
stormy-gold•2mo ago
it might not be document we d explicitly. can be improved for sure
but you should have gotten a typescript error
did you not?
genetic-orangeOP•2mo ago
This is what i see.


genetic-orangeOP•2mo ago
My typescript intellisense is broken. I think i messed up the configuration somewhere a bit. Because i also do not get route auto completes when using navigate hook.
stormy-gold•2mo ago
then fix the typescript setup first 😆
genetic-orangeOP•2mo ago
After moving some code around and declaring the module i do get intellisense. Which would have indeed prevented the problem in the 1st place.
Is it possible to instantiate the router in a component, while still being able to use Context.Provider components?
Because of this i was not able to then declare the module because router was not in scope anymore + you cannot declare the module in function scope.
Or is it a better idea to completely migrate these context components into the Router context?
Thanks by the way for all the help. The intellisense does work after refactoring stuff to get the module declaration to work.
stormy-gold•2mo ago
I would not instantiate the router inside a component
what's the use case for that?
genetic-orangeOP•2mo ago
This project uses a lot of useContext hooks, with Context.Provider components.
So underlying components can
const someHook = useMyHook
These are the .Provider components for that.
In order to pass this context to the router, so i can use authorization in beforeLoad. I use the hook in a router component so i can pass it to the initial context of the router, otherwise i get key not defined errors when i navigate to pages.
The problem with this method was that useAuth for example can only be called in Provider component context, so i had to wrap the router instantiation inside a Router component, with the context Provider components as their parents.
Problem was that you now cannot infer the type of router, because its instantiated in component and module block declarations do not work there.
At least that was my thought process there.stormy-gold•2mo ago
you can supply context via the RouterProvider. also look at Wrap and InnerWrap options
genetic-orangeOP•2mo ago
Final question then i hope to not bother u anymore. Is this the correct way of doing things? When i use Wrap, i still cannot use the
useAuth hook to create context, because my provider for useAuth is in AppProviders i tried it but it didnt work. This way everything down the tree can use all the providers, including the router.stormy-gold•2mo ago
Wrap works like this: (taken from packages/react-router/src/RouterProvider.tsx)
So it wraps the RouterProvider, not App in your case.
Your impl looks fine!