Shared route components #2084
-
My company has several pages where multiple components are rendered on a page. Each section has a sidebar item and clicking on the sidebar scrolls the user to that part of the page. This is implemented by having all components render in a single route component, but when a particular sub-route is matched then we scroll to that component. So for example, our const RouteComponent = () => {
return (
<SidebarPage>
<Overview active="/service/overview" />
<Billing active="/service/billing" />
</SidebarPage>
)
} When converting to Tanstack router, we figured we can keep this pattern but to keep type safety on the routes, instead of making the last param a dynamic path segment (and only having one route), we have a route for each section and each route just shares the same component. So our routes look like this instead: export const OverviewRoute = createRoute({
getParentRoute: () => ServiceRoute,
path: 'overview',
component: RouteComponent,
});
export const BillingRoute = createRoute({
getParentRoute: () => ServiceRoute,
path: 'billing',
component: RouteComponent,
}); Great. This worked, until the somewhat recent changes to memoize routes based on what part of the route has changed. When this was implemented, it means that this shared component remounts when changing routes which then triggers any of our logic that executes when the component is first mounted. It seems like this is because a key props is passed to the route component and this changes which tells react it's a new component (and hence a re-render). This is... not ideal, especially at this late stage in converting our application. Going back a couple of versions is an option, but if the "problem" is never going to get resolved then we obviously don't want to lock ourselves to this version of the router and I'm looking for options. Main Questions:
To be honest, I considered changing up the routing to use a route hash for each thing on the page... This makes the most sense, but unfortunately there is no built in type safety for route hashes that I can see. Would having something similar to search params be possible? At the moment there is Thank you to anyone who has any ideas in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
we are working on the remounting issue. |
Beta Was this translation helpful? Give feedback.
-
Thank you for letting me know! I've just told my company we are moving to hash routes anyway since using full routes for the same page really doesn't make sense. Is a |
Beta Was this translation helpful? Give feedback.
we are working on the remounting issue.
as of https://github.com/TanStack/router/releases/tag/v1.46.7, the remounting is fixed, however the component renders twice instead of once, so we need to understand why that is.