Framer Motion AnimatePresence Not Working — Fix Exit Animations
In this fix, you'll learn how to animate element removal with AnimatePresence. This matters for smooth content transitions, and appears in modal dialogs and tab panels.
The Problem
The following code demonstrates the typical mistake:
{isOpen && <motion.div>Content</motion.div>}
// Content disappears instantly when isOpen becomes false
The Fix
Here is the corrected version:
import { AnimatePresence, motion } from 'framer-motion';
<AnimatePresence>
{isOpen && (
<motion.div
initial={{ opacity: 0, y: -20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
>
Content
</motion.div>
)}
</AnimatePresence>
// Content animates out before being removed from DOM
Prevention
- Wrap conditional elements in AnimatePresence for exit animations.
- Always define an exit prop on the motion element.
- Place AnimatePresence around the toggle point, not nested deeply.
- Use mode='wait' to wait for exit before entering new elements.
- Use onExitComplete callback to run logic after animations finish.
Common Mistakes with framer motion animate presence
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro