Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The element vanishes at once instead of animating out

ErrorThe element disappears instantly and the exit prop never runs

Short answer

AnimatePresence plays an exit animation only when a direct child with a unique key leaves the tree while AnimatePresence itself stays mounted. Move the condition inside it, give the child a stable key, put an exit prop on the motion component, and use mode wait when swapping a single child.

  • Applies to motion (motion/react), framer-motion, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the JSX around the element that leaves. Note where the condition is, what the keyed element is, and which package is imported.

    grep -rn "AnimatePresence" src
    npm ls motion framer-motion
  2. 2Match what you see, then jump to its fix

    1. 1The code reads show && <AnimatePresence> ...The condition wraps AnimatePresence instead of sitting inside itFix
    2. 2Lists use the array index as key; or a page swap has no key at allThe child has no key, or an unstable oneFix
    3. 3A plain div or fragment sits between AnimatePresence and the motion componentThe keyed element is nested, so it is not a direct childFix
    4. 4The new element appears while the old one is still on screen, or both overlapmode is not suited to swapping one view for anotherFix

What does the error look like?

  • The element disappears instantly and the exit prop never runs
  • A list item or modal has no leaving animation, though the entering animation works

Cause 1 of 4

The condition wraps AnimatePresence instead of sitting inside it

1Check

The docs say AnimatePresence must be outside the code that unmounts the element. When the whole thing unmounts, nothing remains to play the exit.

grep -rn -B2 "<AnimatePresence" src | grep "&&\|?"

2Fix

Keep AnimatePresence always rendered and put the condition inside it.

<AnimatePresence>
  {isVisible && (
    <motion.div key="modal" exit={{ opacity: 0 }} />
  )}
</AnimatePresence>

3Verify

Toggle the condition and watch the fade.

npm run dev

Confirmed by motion.dev

Did this fix it?

Cause 2 of 4

The child has no key, or an unstable one

1Check

Direct children need a unique key so the component can tell which one left. Index keys break when items reorder.

grep -rn "key={index}\|key={i}" src

2Fix

Use an id that belongs to the item.

<AnimatePresence>
  {items.map((item) => (
    <motion.li key={item.id} exit={{ opacity: 0 }} />
  ))}
</AnimatePresence>

3Verify

Remove the first item in the list and check that it animates out.

npm run dev

Confirmed by motion.dev

Did this fix it?

Cause 3 of 4

The keyed element is nested, so it is not a direct child

1Check

AnimatePresence watches its direct children. The motion element can sit inside your own component, but that component must be the child that carries the key.

grep -rn -A3 "<AnimatePresence" src

2Fix

Put the key on the immediate child, and the exit prop on the motion component inside it.

<AnimatePresence>
  <Slide key={activeItem.id} />
</AnimatePresence>

3Verify

Change activeItem and confirm the old slide leaves with its exit animation.

npm run dev

Confirmed by motion.dev

Did this fix it?

Cause 4 of 4

mode is not suited to swapping one view for another

1Check

The default mode is sync, so the entering and leaving elements share the screen. wait mode supports one child at a time.

grep -rn "mode=" src

2Fix

Set mode to wait when only one element should be visible at a time.

<AnimatePresence mode="wait">
  <motion.div key={route} exit={{ opacity: 0 }} />
</AnimatePresence>

3Verify

Switch views; the new one enters after the old one has left.

npm run dev

Confirmed by motion.dev

Did this fix it?

How do I stop it coming back?

  • Treat AnimatePresence as a permanent wrapper and put every condition inside it.
  • Use database or list ids as keys, never the index.
  • Import from one package only: motion/react for the current Motion package.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.