AnimatePresence enables exit animations for components being removed from the React tree.
Basic Usage
Wrap components withAnimatePresence and define exit animations:
Props
initial
Control whether children animate on initial mount:
- Preventing animations on page load
- Server-side rendering
- Modals that shouldn’t animate on first render
mode
Controls how entering and exiting elements are handled:
"sync" (default)
Elements animate in and out simultaneously:
"wait"
Wait for exiting element to finish before animating in the next:
"popLayout"
Exiting elements are “popped” from page layout, allowing siblings to move immediately:
onExitComplete
Callback fired when all exiting elements have finished animating:
custom
Pass dynamic data to exit animations:
presenceAffectsLayout
Control whether sibling elements re-render during exits (internal, defaults to true):
Multiple Children
When animating multiple children, each must have a uniquekey:
Exit Variants
Use variants to orchestrate complex exit animations:Nested AnimatePresence
Use thepropagate prop to propagate parent exit animations to nested children:
Common Patterns
Modal Dialog
Notification List
Route Transitions
Advanced: usePresence Hook
For custom exit animations outside of motion components:useIsPresent
Simpler hook that only returns presence state:Important Notes
- Keys are required for AnimatePresence to track components
- Direct children only - AnimatePresence only tracks direct children
- Exit animations - Components need an
exitprop to animate out - Conditional rendering - Use conditional rendering, not
display: none - Multiple children - Each child needs a unique key when rendering lists