Confusing useRouteContext behavior within an errorComponent
#6258
Unanswered
JakeHaitsma
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand the expected behavior of
useRouteContextwithin anerrorComponent.I'm loading current user information in my
__rootroute'sbeforeLoad, and I'd like to display some of that user information in error components throughout my application.When a descendant route's
beforeLoadthrows an error, theerrorComponentfor that route can calluseRouteContext(), but some of the parent context properties are undefined, even though the parent'sbeforeLoadcompleted successfully. Interestingly, context provided via the React<RouterProvider context={...}>is accessible.Here's an example StackBlitz to demonstrate what I'm talking about: https://stackblitz.com/edit/github-4kchyyu8?file=src%2Froutes%2Findex.tsx (note also that when a module is hot replaced on edit, both the
RouterProvidercontext property and the__rootcontext property are available, but I'm less interested in digging into this because that's not something that will happen in production).Questions:
I'm guessing that using
useRouteContextinerrorComponentdoesn't make sense and this is just user error (at least, the types seem to suggest this). Regardless, I'd appreciate any guidance on the expected behavior and best practices!Beta Was this translation helpful? Give feedback.
All reactions