Replies: 7 comments 1 reply
-
Have you found a way to integrate AnimatePresence? I'm currently running into the same issue |
Beta Was this translation helpful? Give feedback.
-
Have you found a solution ? |
Beta Was this translation helpful? Give feedback.
-
I found a working solution here: https://stackoverflow.com/a/74352029 |
Beta Was this translation helpful? Give feedback.
-
@ryanflorence Any idea what's the way forward here? Not really sure how to do transitions anymore. |
Beta Was this translation helpful? Give feedback.
-
I looked for hours and this is the way: https://codesandbox.io/s/animated-nested-routes-demo-react-router-v6-forked-lxf1bf?file=/src/index.tsx:382-474 I'm actually shocked that it works... |
Beta Was this translation helpful? Give feedback.
-
I have been looking for a way to approach this issue without having to rely on the Route component syntax at all. It is a little different to the original question but the solution could be similarly applied. Below is a simplified version of the routing problem I am looking at. I want to animate between the index child route and the "/settings" route.
To control the exit animations, I am going to have to use the AnimatePresence component. Putting the component in any of the child routes doesn't work as child routes unmount instantly on navigation including AnimatePresence. It can't delay any unmounting if itself is unmounted. So the AnimatePresence component needs to persist between child navigations, which suggests it should go in the GlobalLayout component (which contains the app footer and header).
Simply wrapping the outlet component in AnimatePresence doesn't seem to work.I don't fully understand why, it might be that the Outlet itself never unmounts and so no animations trigger. It could be that the Outlet key prop never changes and so it can't be tracked by AnimatePresence, or it could be that the Outlet wraps the rendered child routes in other components, so rendered routes are not direct children of AnimatePresence. Either way, I couldn't get AnimatePresence to track changes in the outlet component. The next question to ask is If AnimatePresence can't track changes, is it possible to manually track changes and trigger animations? Changes in routes occur during navigation, so we can just use react-router's useLocation hook to track changes to the URL. AnimatePresence tracks the keys of direct descendants, and if the key changes, then it interprets this as an unmounting of an old component, and a remounting of a new component. We can use this key change behaviour to manually trigger entry/exit animations, without unmounting a component. Therefore, If we wrap the Outlet Component in a motion.div, and have the key prop of motion.div be unique to the route, then navigating to a new route, changes the key and triggers the exit/entry animations. This simulates the page transitions I was looking for. Below was some simple code I used to test out the method.
Back to the original question, you may be able to add a global layout route that can handle the page transitions using this "Outlet wrapping" method? |
Beta Was this translation helpful? Give feedback.
-
This was "good enough" for us.
Looks fairly seamless unless rapidly navigating. We had to remove any |
Beta Was this translation helpful? Give feedback.
-
Hello, I am currently using framer-motion version 10.12.4 and react-router-dom version 6.10.0 for handling page transitions. When changing pages, the new page comes in from the right and the old page goes out to the left. Here's the working code I have:
The problem with this code is that it does not allow me to use all the new features in version 6.4+ such as loader and action. If I want to use these new features, my code should look like this:
However, with this code, I am unable to trigger the AnimatePresence effect, and I am unsure where to place it. Can someone please advise me on where to put the AnimatePresence?
Beta Was this translation helpful? Give feedback.
All reactions