I recently jumped the gun on upgrading to the latest react-router-dom and their fancy new Data APIs, and this essentially meant rewriting my app's routing logic using the new object-based routing instead of the V5 JSX syntax with Routes. I was wondering if anyone here has managed to get route transitions working with react-router V6?
In V5 it was possible to have nested route animations declared in the following manner:
<AnimatePresence>
<Routes location={location} key={locationArr[1]}>
<Route path="/" />
<Route path="/nested/*">
<AnimatePresence>
<Routes location={location} key={locationArr[2]}>
<Route path="/nested/1" />
<Route path="/nested/2" />
</Routes>
</AnimatePresence>
</Route>
</Routes>
</AnimatePresence>
Managing the nested Routes object key this way allowed to perform page transitions only on the changed nested route: the parent route would not re-render when navigating to different child routes. But now with V6, Routes-object is no more, and I’m having some troubles implementing the same functionality as in V5.
I’ve created a codesandbox to try and replicate V5 nested child route animation functionality. The sandbox uses a stable useOutlet reference for both parent and nested routes, following discussion at https://github.com/remix-run/react-router/discussions/8008#discussioncomment-1280897.
Codesandbox: https://codesandbox.io/s/cool-cherry-wjrvhl?file=/src/App.tsx
Using latest react, react-router-dom, typescript and framer-motion.
In the sandbox it’s visible that the parent route animations work as expected, but the nested routes will not render at all, presumably because the parent Outlet reference is frozen. Only way to make the content update is to refresh the page. Changing RootLayout’s key from locationArr[1] to location.pathname will re-render the nested routes correctly, but this means that the whole page layout rerenders, which is to be expected but not desirable: my app has a Header component that is visible on all child routes, and I'd like that only the Outlet would animate when navigating between nested child routes while the Header stays visible and stationary during the child route animation.
Desired result would be identical to this sandbox that does not use the Data APIs: https://codesandbox.io/s/animated-nested-routes-demo-react-router-v6-forked-lxf1bf
I'm starting to think that this might not be possible with the V6 data API structure, but curious to see if anyone else has been struggling / coming up with solutions for this. All help appreciated!
Routecomponents work.createBrowserRouter. The code sandbox has my attempt at making route animations work with the new syntax.