On this page
React removes elements immediately, so there's normally no moment to animate them out. Motion's `AnimatePresence` creates that moment: it keeps a removed child on screen until its `exit` animation finishes. When an exit animation silently doesn't play, it's almost always one of a handful of mistakes, each with a one-line fix.
The pattern that works
AnimatePresence stays mounted, and the element inside it is what comes and goes. The element needs an exit prop describing how it leaves:
"use client";
import { AnimatePresence, motion } from "motion/react";
export function Notice({ show }: { show: boolean }) {
return (
<AnimatePresence>
{show && (
<motion.div
key="notice"
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -8 }}
>
Saved
</motion.div>
)}
</AnimatePresence>
);
}Mistake 1: AnimatePresence is inside the condition
If AnimatePresence is removed along with its child, there's nothing left to run the exit. This is the most common cause:
// ✗ AnimatePresence disappears with the element
{show && (
<AnimatePresence>
<motion.div exit={{ opacity: 0 }} />
</AnimatePresence>
)}
// ✓ AnimatePresence stays, the element inside it goes
<AnimatePresence>
{show && <motion.div key="box" exit={{ opacity: 0 }} />}
</AnimatePresence>Mistake 2: missing or unstable keys
AnimatePresence tracks its direct children by key. Without one, swapping one element for another looks like an update rather than a removal, so nothing exits. In lists, using the array index as the key makes the wrong item animate out, because keys shift when an item is removed. Use an id that belongs to the item:
<AnimatePresence>
{items.map((item) => (
<motion.li key={item.id} exit={{ opacity: 0, height: 0 }}>
{item.label}
</motion.li>
))}
</AnimatePresence>To swap content in place, like the steps of a form, change the key and the old step exits while the new one enters.
Mistake 3: the old and new content overlap
By default, the entering element appears while the old one is still leaving. For swaps, choose a mode:
mode="wait": the new element waits until the old one has finished exiting. Best for swapping one thing for another, like steps or tab panels.mode="popLayout": the exiting element is taken out of the layout straight away, so siblings move into its space while it animates out. Best for lists and stacks, paired with thelayoutprop on the siblings.
<AnimatePresence mode="wait" initial={false}>
<motion.div
key={step}
initial={{ opacity: 0, x: 24 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -24 }}
transition={{ duration: 0.2 }}
>
<Step number={step} />
</motion.div>
</AnimatePresence>initial={false} stops the first step from animating in when the page loads.
Mistake 4: expecting it to work between Next.js routes
Wrapping a layout's {children} in AnimatePresence doesn't animate page exits in the App Router, because the router replaces the old page before Motion can keep it. For transitions between pages, use React's <ViewTransition>, which snapshots the old page instead: see page transitions in the Next.js App Router.
Exit animations without JavaScript
For elements the browser shows and hides itself, such as <dialog> and popovers, CSS can now animate both ways. transition-behavior: allow-discrete lets display wait for the transition, and @starting-style sets where the entrance begins:
dialog {
opacity: 0;
translate: 0 8px;
transition: opacity 0.2s, translate 0.2s, display 0.2s allow-discrete, overlay 0.2s allow-discrete;
}
dialog[open] {
opacity: 1;
translate: 0 0;
}
@starting-style {
dialog[open] {
opacity: 0;
translate: 0 8px;
}
}Anything React removes from the page still needs AnimatePresence. For a working example of exits in a list, the Notification Bell and Tag Input components both use it.
Reduced motion
Wrap your app in <MotionConfig reducedMotion="user"> and Motion drops transform and layout animations for people who ask for reduced motion, while keeping opacity fades. Exits then become quick fades instead of movement.
Questions
Why is my Framer Motion exit animation not working?
Usually because AnimatePresence is inside the condition that removes the element, or the element has no key. Keep AnimatePresence mounted, put the condition inside it, and give each child a stable key.
Does the exit prop work without AnimatePresence?
No. Without AnimatePresence, React removes the element immediately and the exit animation never starts.
What's the difference between mode="wait" and mode="popLayout"?
wait holds the new element back until the old one has left, which suits swapping content. popLayout removes the old element from the layout immediately so the rest of the page can reflow while it animates out, which suits lists.
Can I animate a page leaving in the Next.js App Router?
Not with AnimatePresence. Use React's ViewTransition, which works with the App Router's navigations.