-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't seem to be working with route objects #62
Comments
There might be ways we can improve this hook to work better with const routes = [
{
path: "/",
element: <AppLayout />,
children: [
{ index: true, element: <HomePage /> },
{
path: "users/*",
element: <UsersPage />,
breadcrumb: UserDetailBreadcrumb
},
{ path: "*", element: <Navigate to="/" /> }
]
}
] as BreadcrumbsRoute[]; export const UserDetailBreadcrumb: BreadcrumbComponentType<string> = ({
match
}) => {
console.log(match);
if (!match.params["*"]) {
return "Users";
} else if (match.params["*"].includes("edit")) {
return "Edit";
}
return <>{usersById[(match.params["*"] as unknown) as UserId]}</>;
}; Again, not the cleanest solution, but that will at least work with the current version of the hook. I'll have a think about ways this could potentially be made better. If the hook detects a |
Thank you for the workaround, will try it out and report back. |
export const UserDetailBreadcrumb: BreadcrumbComponentType<string> = ({
match
}) => {
console.log(match);
if (!match.params["*"]) {
return "Users";
} else if (match.params["*"].includes("edit")) {
return "Edit";
}
return <>{usersById[(match.params["*"] as unknown) as UserId]}</>;
}; Writing this alone outputs:
Are we missing something? |
you could wrap the strings with a |
Thanks @gaspardip - yea types could be better for this |
Thanks @Nodios! I think that change might make sense as an optional param, but it unfortunately I don't think it solves @gaspardip's original issue (it might make it a bit nicer to work with, though)... I think to solve the core of this, we need to somehow know the nested route config within |
We might be able to do something like It might cause unexpected issues, so I'll have to investigate, but this could be a way where the hook could check the This is within App in @gaspardip's example. As you can see in the deep object I can find the |
Here's an experimental PR with support for nested route objects within I'll run some more tests on this approach later, but I really wish |
@icd2k3 Did you consider lazy loaded routes? To be fair I did not test your changes yet, but I am unsure how it'll track nested route objects for the lazy loaded routes. I assume you'll have to either register everything beforehand or have some kind of a mechanism in place that'll enable to you add configuration in a lazy manner. |
@Nodios yea lazy loading will probably be a separate issue - I see in their example they wrap the Of course, this hook will still generate default breadcrumbs for those lazy routes, and users can override their own in this hook's config, but it would be nicer to keep everything encapsulated like @gaspardip wants. All of this would be pretty trivial if |
I updated the PR for support of nested lazy-loaded elements with their own route objects, but as you can see the code is pretty ugly to get into that config we need... will have to do a lot of different component-type testing to make sure there's no edges. |
@icd2k3 thank you so much for taking the time to follow this up, it does indeed feel kinda hacky and as @Nodios pointed out, my real app also uses lazy loaded routes, so that would also need to be considered. |
I asked around on discord and someone shared this with me
I still don't know if this covers nested lazy-loaded route trees |
@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. |
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 additionalchildren
routes likeedit
.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 theUsersPage
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 toUsersPage
.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?
The text was updated successfully, but these errors were encountered: