Skip to content

[BUG] AnimatePresence exit animations permanently stop working after its component suspends #2690

@justintoman

Description

@justintoman

1. Read the FAQs 👇

2. Describe the bug

If a component that renders an AnimatePresence ever suspends, the exit animations don't work again. I'm not talking about animations that get interrupted by a suspense, but animations that happen at any point in time after the AnimatePresence was suspended and then resumed.

3. IMPORTANT: Provide a CodeSandbox reproduction of the bug

Code Sandbox Link

4. Steps to reproduce

Steps to reproduce the behavior:

Consider these components

function App() {
  return (
    <Suspense fallback={<p>Suspended</p>}>
      <MyComponent />
    </Suspense>
  );
}

function MyComponent() {
  const [items, setItems] = useState(() =>
    ['alice', 'bob', 'carol'].map(name => ({ id: crypto.randomUUID(), name })),
  );
  return (
    <ul>
      <AnimatePresence>
        {items.map(item => (
          <motion.li
            key={item.id}
            initial={{ x: 100 }}
            animate={{ x: 0 }}
            exit={{ x: -100 }}
          >
            {item.name}
            <button onClick={() => setItems(items.filter(i => i !== item))}>
              x
            </button>
          </motion.li>
        ))}
      </AnimatePresence>
      <button
        onClick={() => {
          /* do something that causes MyComponent to suspend */
        }}
      >
        Suspend
      </button>
    </ul>
  );
}
  1. Remove an item, the exit animation works
  2. Cause the component to suspend
  3. Now try removing an item, the exit animation does not work

(The code sandbox is a better demonstration ☝️ )

5. Expected behavior

Exit animations continue to work after the component has resumed after a suspense

6. Video or screenshots

Screen.Recording.2024-05-30.at.4.47.01.PM.mov

7. Environment details

  • Chrome 125.0.6422.78
  • MacOS Sonoma 14.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions