React unmounts a removed component synchronously — the DOM node is gone before any exit animation could visibly play. AnimatePresence solves this by intercepting the unmount: it keeps the outgoing component mounted in the DOM just long enough to run its exit animation, then removes it for real once that animation finishes.
Basic usage
import { AnimatePresence, motion } from "framer-motion";
function Modal({ isOpen }) {
return (
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0, scale: 0.9 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.9 }}
/>
)}
</AnimatePresence>
);
}Why exit is inert without this wrapper: exit (from animate props) is just a prop on motion.div — it needs something watching the React tree for removals and delaying the actual unmount. AnimatePresence is that watcher; it inspects its direct children on every render and detects when one has been removed from the list, rather than the removed component detecting its own removal (which is impossible — it’s already gone).
Requirement: a stable key
AnimatePresence identifies “this specific child was removed” (as opposed to “this child’s content changed”) via React’s key prop — this matters most when swapping between multiple children, not just a single conditional one:
<AnimatePresence mode="wait">
<motion.div key={currentTab} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>
{tabContent[currentTab]}
</motion.div>
</AnimatePresence>Without a distinct key per tab, React would treat this as “the same element, content updated” rather than “old element removed, new element added” — and no exit/enter transition would fire at all.
mode prop
"sync"(default) — exiting and entering elements animate simultaneously (bothexitand the new element’sinitial→animaterun at once)"wait"— the exiting element fully finishes itsexitanimation before the new element starts entering (used above for tab content, where overlapping would look like double content)"popLayout"— removes the exiting element from document flow immediately (viaposition: absolute) so sibling elements can animate into its space right away, while the exiting element still plays its own exit animation on top — used for animated list removal where remaining items should slide up immediately rather than waiting
List example (add/remove with exit)
<AnimatePresence>
{items.map((item) => (
<motion.li
key={item.id}
layout
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: "auto" }}
exit={{ opacity: 0, height: 0 }}
>
{item.label}
</motion.li>
))}
</AnimatePresence>The layout prop here is what makes the remaining items smoothly slide up to fill the gap when one is removed — that’s a separate mechanism covered in layout animations.
Related
- animate props — what
exitactually is - variants — variants commonly carry the
exitstate as well ashidden/visible - layout animations —
layoutprop for animating position changes among siblings
Software engineering
Infrastructure & security
Computer science
Hardware & engineering