motion: [BUG] AnimatePresence gets stuck when state changes quickly
2. Describe the bug
Framer Motion v11.0.12: When state changes very quickly AnimatePresence gets stuck and out of sync and no longer removes the element but instead always animates it in. Reverting to version v10.12.2 (based of other bug report concerning a similar/same issue) does not exhibit the same problem
3. IMPORTANT: Provide a CodeSandbox reproduction of the bug https://codesandbox.io/p/sandbox/serene-brattain-qwd278
4. Steps to reproduce
- Conditionally render an element inside AnimatePresence with an exit animation
- Trigger condition change quickly
- Animating component gets stuck as rendered, and any changes to the condition will just trigger an reset from initial => animate. The component is no stuck as visible and will never leave the DOM.
5. Expected behavior
The component should not get stuck as mounted
6. Video or screenshots
If applicable, add a video or screenshots to help explain the bug.
7. Environment details
Chrome 122, Mac Sonoma
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 47
- Comments: 57 (1 by maintainers)
Commits related to this issue
- fix(ui): stuck viewer when spamming toggle There are a number of bugs with `framer-motion` that can result in sync issues with AnimatePresence and the conditionally rendered component. You can see t... — committed to invoke-ai/InvokeAI by psychedelicious 3 years ago
- fix(ui): stuck viewer when spamming toggle There are a number of bugs with `framer-motion` that can result in sync issues with AnimatePresence and the conditionally rendered component. You can see t... — committed to invoke-ai/InvokeAI by psychedelicious 3 years ago
- fix toggle inconsistency between date (related framer/motion#2554) — committed to DevKor-github/devkor.club-front by hyp3rflow 2 years ago
My suspicion given that this always(?) involves
opacityand switching back to a previously existing component, that there’s a bug in how WAAPI animations resolve after being interrupted.So I’m going to write a browser test that encapsulates this behaviour and then figure out a fix.
thanks,11.0.10 works for me. After two days of repeated debugging, none of the methods mentioned in the other issues worked Only downgrading the version was effective. This bug has been around for several years since version 9.x?
Here are two links. The code is the same except for the version of framer-motion. 11.2.12: https://codesandbox.io/p/devbox/framer-motion-11-2-12-s2rwgt 11.0.10:https://codesandbox.io/p/devbox/framer-motion-11-0-10-6f43vg
https://github.com/framer/motion/assets/13536658/b5af3987-74b2-448d-9fde-9c317f507724
11.2.12
https://github.com/framer/motion/assets/13536658/4b5c9c10-d327-40ea-9a80-ef402d1807e4
11.0.10 works
Curious if there is an update here - feels like adding and removing an element quickly with animate presence is quite common and should be supported without any hacks.
I have same issue here, but I solve it in a silly way.
add
Math.random()in keyI think is the exit props hold this component from unmount but in react this component mount again, so now framer-motion read two component having same key, somehow break the view.
if there is any better solution ?
@RareSecond This seems to solve the problem of “deleting” the object, but there’s still something wrong with it With opacity:
Without opacity:
This appears to have been broken in
11.0.11. Last version that doesn’t suffer from this issue is11.0.10.This is now the most commented on and “liked” issue (even more if you count the duplicates). Would be cool to at least get a comment on if its being looked into.
Am experiencing the exact same issue, but in a different setup. There seem to be cases where exit is never called, and the potential for it not being called seems to be indeed tied to opacity. If I remove the opacity on my exit, it will always get called.
Here’s an example. If you slowly hover over the dots on the right, it works as expected. However, once you start going faster, it sometimes bugs and multiple divs remain visible
Important part is that, if you remove
opacityfrom the exit animation, it will work perfectly fine10.12.2does not work for me,11.0.10doesI have the same problem and still could not find the solution. It happened after version 11.0.8. Here is my example and code below.
https://github.com/framer/motion/assets/40694757/2d020827-8537-466c-a1eb-6bd3414be27f
sorry about that, https://codesandbox.io/p/sandbox/serene-brattain-qwd278 is now public
My use case was Page Transitions and adding the
keyto the<Routes />was causing issues. I tried changing it to different things, but it persisted. Weirdly enough, removing it fixed my issue.@tnfAngel @Flamenate
AnimatePresence works by detecting when Direct Child are removed from the React tree.
base on framer-motion 11.3.4. when used in conjunction with radix-primitive, the same usage behaves inconsistently across different Radix-primitives components. For radix-primitive components that use the asChild prop, sometimes they can be used directly as child nodes, and other times they cannot.
Placing a motion.div as a direct child at the appropriate position can solve the problem for most cases.
Below are examples using radix-accordion, radix-context-menu, and radix-tooltip. link: https://codesandbox.io/p/devbox/framer-motion-11-3-4-34sjjk
video:
https://github.com/user-attachments/assets/ca4a5d8d-a318-44fa-a276-17165b62d62c
These are some methods I have tried, which have solved all the issues I encountered:
I am not sure about your use case, but if this does not help you, please create a sandbox link.
Downgrading to 10.12.2 fixed the issue for me
I am encountering this issue again with NextJS in the latest (11.18.x). Removing layoutID from the nested children allows the animate presence to work. Would a reproduction be helpful?
In the meantime, is there any troubleshooting I can do?
My god. Can confirm that removing ‘key’ attribute from <motion.div> resolves the issue for latest version - 11.14.4
I also had issue related to changing direction of the animation. Whenever the direction was changed e.g from left to right and from right to left - the exit animation would not be displayed. Guess what - removing key attribute also resolved this issue! So for everyone that is struggling with their animations with directions please try removing the key attribute from your motion.div!
“framer-motion”: “^11.11.9”
This animation is used in a tap game. When I tap fast and a lot, game app freezes. Any idea?
Same Issue: https://github.com/framer/motion/issues/2693
I can also confirm that the suggestion above with removing the exit opacity works.
Good find! Using count as key can hot fix this for now
const [count, setCount] = useState(0);or reverting to 11.0.10 works as well.I have the same issue. https://codesandbox.io/p/devbox/great-brattain-hc9kgz but i found using non-repeating
keycan solve it.Fix it to 11.0.10
Correct. Just checked!