useMatches hook not returning data from my route handle #9710
-
Hi there, I'm trying to build a breadcrumb component using the new My code looks a little like this... App Routes.tsx
PublicRoutes.tsx
ProtectedRoutes.tsx
Feature1.tsx
To explain the code above slightly, I have the toplevel The protected routes then import defined routes from each indivudual feature, which is a paradigm I prefer as I like to keep all code related to a feature in one place. I'm wondering wether the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yep, you've identified the underlying issue :). Descendant routes are not known to The recommendation would be to lift your route definitions up to the initial route tree and then use code splitting to lazily load the loaders/actions/elements for those routes. There's a good community article on this here. Then you can check for protected routes in your loaders. For now, you'll want to check in each loader until this proposal gets implemented and then you will be able to check for your protected routes in a single parent |
Beta Was this translation helpful? Give feedback.
Yep, you've identified the underlying issue :). Descendant routes are not known to
useMatches
(docs) since matching in aRouterProvider
is done before rendering, and descendant<Routes>
are not discovered until rendering.The recommendation would be to lift your route definitions up to the initial route tree and then use code splitting to lazily load the loaders/actions/elements for those routes. There's a good community article on this here.
Then you can check for protected routes in your loaders. For now, you'll want to check in each loader until this proposal gets implemented and then you will be able to check for your protected routes in a single parent
beforeLoader
-type function.