Undefined context with useRouteContext
Hi !
I have an issue with the
useRouteContext hook coupled with validateSearch in a nested route that throw an error in a specific situation.
In my application, there is an _auth route that ensure, in the beforeLoad method, that the user is logged in, returns his account's information if he is or redirects him to the login page if he's not.
There's also a route /_auth/events that can have three optional search params (page, type & criticality, by default, 1, "all", "all"). When I navigate to /events from within the application, everything works as expected, the url change to /events?page=1&type=all&criticality=all as expected, but if I directly type /events in my browser, the url is also correctly updated, but I get the error below (context is undefined in the AuthLayout while being correctly logged in the beforeLoad method).
Did someone already faced this issue ?


4 Replies
extended-salmonOP•2y ago
Also, if I remove
validateSearch, everything works fine, so the issue must come from there.
I tried to use loader and the useLoaderData hook in the _auth route, same thing.
Same error when I use useRouteContext({ from: '/_auth' }) instead of Route.useRouteContext().
If I replace .catch() by .optional(), it works, but not with .default(). Basically, I feel like the bug comes from the rewrite of the url when the params are not present.absent-sapphire•2y ago
this will most likely be fixed by PR #1907
absent-sapphire•2y ago
your issue sounds like https://github.com/TanStack/router/issues/1656
GitHub
Context is undefined when search validation fails · Issue #1656 · T...
Describe the bug I've discovered that the missing context bug isn't fully fixed in 1.33.7. Also sorry for sort of spamming the other closed issue, I thought I'd open a new one. I'm ...
extended-salmonOP•2y ago
Thanks, I'll keep an eye on this then !