use-react-router-breadcrumbs: Doesn't seem to be working with route objects
If I understood correctly, I should be able to use my top level routes (app routes) as a param for the hook and correctly get all app breadcrumbs according to the routes definitions. The hook should walk down the tree and resolve children routes and their respective breadcrumbs in the process. I found that’s not the case when doing the following: https://codesandbox.io/s/spring-fog-lbxg0g
In this sandbox, I want every route under /users/:userId
to have a dynamic breadcrumb mapping to the user name, but I don’t want to explicitly define each of those routes with the /users/:userId
prefix (say /users/:userId/edit
), so I take advantage of react-router and define a :userId
route, then define some additional children
routes like edit
.
The only way I found to get this behavior is to explicitly define another routes array with a path
that partially matches the one I’m interested in and pass that to the hook.
This is not ideal since I want to reuse my routes
definitions in a couple of places in my app, including the app itself (useRoutes
), a navbar, and the breadcrumbs trail. It also defeats the “encapsulation” of the UsersPage
component since the breadcrumbs trail now needs to know about the :userId
route at the top level, while react-router allows me to keep that something private to UsersPage
.
Note that using { disableDefaults: true }
generates no breadcrumbs at all in the first case, while it only generates the user name breadcrumb in the latter.
Am I missing something obvious here? or is my approach wrong?
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (8 by maintainers)
Commits related to this issue
- feat: add route object support (#62) — committed to Nodios/use-react-router-breadcrumbs by Nodios 2 years ago
@gaspardip ohhh I haven’t seen this before! Thanks for asking around, and bringing it to my attention!
This hook was created because (before 6.4) there wasn’t really a straightforward way of implementing breadcrumbs with react-router. Since it’s now “officially” supported in their docs, it might be time to suggest folks use their approach (if they’re on 6.4+).
I should have some time to try it out and evaluate next week. I assume their example works with useRoutes and lazy-load too.
If it works well I’ll probably change the docs here to suggest people use the 6.4 method instead.
you could wrap the strings with a
Fragment
so ts doesn’t complain