The exit Prop and Unmount Timing
What you'll learn: How the exit prop controls the animation that plays when a component is removed, and why React delays the actual DOM removal until that animation completes.
The Problem: Instant Disappearance
By default, when you conditionally unmount a React component (like {show && <Component />}), it vanishes from the DOM immediately. There's no time for a fade-out or slide-away animation.
The Solution: exit + AnimatePresence
Framer Motion's exit prop defines the animation state a component should animate to before unmounting. When wrapped in AnimatePresence, React postpones the actual DOM removal until the exit animation finishes.
<AnimatePresence>
{show && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
I fade out before leaving
</motion.div>
)}
</AnimatePresence>
How Unmount Timing Works
Think of it like an actor leaving a stage: without exit, the actor instantly teleports away. With exit, the actor gracefully walks off stage, and only then do the stagehands clear the props.
- Condition becomes false (
show = false) - Framer Motion detects the change via
AnimatePresence - Component animates to the
exitstate (e.g., opacity 0) - Animation completes
- React removes the component from the DOM
The exit prop accepts the same values as animate: objects with style properties, or variant names if you're using the variants pattern.
Key Takeaway: The exit prop defines your unmount animation, and AnimatePresence holds the component in the DOM just long enough for that animation to complete before React removes it.