T
TanStack13mo ago
ambitious-aqua

Prevent router from re-mounting identical components from different paths

Is it possible to prevent the router from re-mounting the entire page when there are identical components in both routes? Using ReactRouter, if two pages have a <Box width={x} transition="all 500ms" /> with different x values, the box would just smoothly change it's width. However, using tanstack-router, the box is unmounted and another one is mounted on top of it, breaking any animation.
18 Replies
ratty-blush
ratty-blush13mo ago
GitHub
router/packages/react-router/src/Match.tsx at 14b6042d6145975e1e31f...
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router
ratty-blush
ratty-blush13mo ago
we added this to avoid unncessary re-renders at the cost of ... remounting upon route change if you have a better idea, feel free to let me know cc @Sean Cassiere
ambitious-aqua
ambitious-aquaOP13mo ago
I think it would be fine if I could choose the key of the route I'm rendering, so that I would be allowed to render different child routes with a same key and prevent unnecessary re-mounts. This could be useful specially if I try to define states on a page based on query params and still reconciliate the component tree with proper animations.
ratty-blush
ratty-blush13mo ago
I just re-tested it without that key and I don't see more re-renderings than with the key
ratty-blush
ratty-blush13mo ago
@King Witcher you can use the PR built package to check if that would solve your issue @Sean Cassiere can you please also have a look at this PR?
wise-white
wise-white13mo ago
We shouldn’t need the key if our fine grained selectors are correct
ratty-blush
ratty-blush13mo ago
I am playing around with the kitchen sink example somehow the InvoiceComponent is rendered twice probably not caused by router I am assuming now
ambitious-aqua
ambitious-aquaOP13mo ago
ofc, i'll test it here
fair-rose
fair-rose13mo ago
How's the component rendering?
ratty-blush
ratty-blush13mo ago
same amount as with the key
fair-rose
fair-rose13mo ago
👍🏼
ambitious-aqua
ambitious-aquaOP13mo ago
sorry for tge delay, I had a blackout in my city but it worked perfectly now! thanks for the answer
ratty-blush
ratty-blush13mo ago
it's released already
ambitious-aqua
ambitious-aquaOP13mo ago
on the main version?
ratty-blush
ratty-blush13mo ago
yes
ratty-blush
ratty-blush13mo ago
GitHub
Release v1.52.1 · TanStack/router
Version 1.52.1 - 8/31/24, 9:10 PM Changes Fix react-router: do not use routeId as key when rendering route component (#2237) (55a2727) by Manuel Schiller Packages @tanstack/react-router@1.52.1 @...
ambitious-aqua
ambitious-aquaOP13mo ago
I was going to ask exactly for that, so thanks again have a nice weekend

Did you find this page helpful?