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

  1. Conditionally render an element inside AnimatePresence with an exit animation
  2. Trigger condition change quickly
  3. 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

Most upvoted comments

My suspicion given that this always(?) involves opacity and 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.

@pklada Reverting to version v10.12.2 solved worked for me.

10.12.2 does not work for me, 11.0.10 does

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.

"framer-motion": "^11.2.11",
"macOS":"14.4.1"

I have same issue here, but I solve it in a silly way.

add Math.random() in key

<AnimatePresence initial={false} mode="popLayout" custom={direction}>
    <motion.section
        key={`${id}-${Math.random()}`}
        className="w-full h-100 relative cursor-pointer"
        style={{
            perspective: '1400px',
        }}
        onClick={() => {
            setIsFlipped((prev) => !prev);
        }}
        variants={sectionMotion}
        custom={direction}
        initial="init"
        animate={['enter', 'show']}
        exit={['exit', 'hide']}
        transition={{
            type: 'tween',
            duration: 0.2,
        }}
    >
        <MotionCard text={back} show={!isFlipped} />
        <MotionCard text={front} show={isFlipped} />
    </motion.section>
</AnimatePresence>;

I 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: framer-presence-issue

Without opacity: framer-bug-bis

This appears to have been broken in 11.0.11. Last version that doesn’t suffer from this issue is 11.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

image

Important part is that, if you remove opacity from the exit animation, it will work perfectly fine

@pklada Reverting to version v10.12.2 solved worked for me.

10.12.2 does not work for me, 11.0.10 does

I 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.

      <AnimatePresence>
                    {open && (
                        <motion.div
                            key="contentMarkContainer"
                            initial={{ opacity: 0, position: 'absolute', bottom: 0, left: 0, right: 0 }}
                            animate={{ opacity: 1 }}
                            exit={{ opacity: 0, transition: { ease: 'easeOut', duration: 0.75 } }}
                        >
                            {My codes here...}
                         </motion.div>
                         )}
        </AnimatePresence>

https://github.com/framer/motion/assets/40694757/2d020827-8537-466c-a1eb-6bd3414be27f

My use case was Page Transitions and adding the key to 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:

  1. Elevate or lower the wrapping scope. Searching on Google for the usage of framer-motion with radix-primitives may reveal incorrect states during fast refresh.
  2. Add a fixed key. Dynamic keys did not help me , even lost the exit animation in a test
  3. Add a motion.div at the outermost layer without adding a key. This is effective and has resolved most of my radix-primitive exit animation issues.

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 use case was Page Transitions and adding the key to the was causing issues. I tried changing it to different things, but it persisted. Weirdly enough, removing it fixed my issue.

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”

<AnimatePresence>
              {animations.map((animation) => (
                <motion.div
                  key={animation.id}
                  initial={{ opacity: 1, y: 0 }}
                  animate={{ opacity: 0, y: -400 }}
                  exit={{ opacity: 0 }}
                  transition={{
                    x: { type: "spring", stiffness: 100 },
                    duration: 0.8,
                    delay: 0.01,
                  }}
                  className="select-none font-medium text-[40px] text-amber-500 touch-none"
                  style={{
                    position: "fixed",
                    left: `${animation.x}px`,
                    top: `${animation.y}px`,
                  }}
                >
                  + 1
                </motion.div>
              ))}
            </AnimatePresence>

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.

I have the same issue. https://codesandbox.io/p/devbox/great-brattain-hc9kgz but i found using non-repeating key can solve it.

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 key can solve it.

Does anyone have a workaround for this? It breaks for us too

Fix it to 11.0.10

This appears to have been broken in 11.0.11. Last version that doesn’t suffer from this issue is 11.0.10.

Correct. Just checked!