Framer Motion Variants Not Propagating — Fix Child Animations
In this fix, you'll learn how to use variants for orchestrated animations. This matters for coordinated multi-element animations, and appears in staggered list animations and page transitions.
The Problem
The following code demonstrates the typical mistake:
const variants = {
visible: { opacity: 1 },
hidden: { opacity: 0 },
};
return (
<motion.div variants={variants}>
<motion.div>Child</motion.div> {/* doesn't animate */}
</motion.div>
);
// Child doesn't fade in — missing variants prop on child
The Fix
Here is the corrected version:
const listVariants = {
visible: { transition: { staggerChildren: 0.1 } },
hidden: {},
};
const itemVariants = {
visible: { opacity: 1, x: 0 },
hidden: { opacity: 0, x: -20 },
};
return (
<motion.ul variants={listVariants} initial="hidden" animate="visible">
<motion.li variants={itemVariants}>Item</motion.li>
</motion.ul>
);
// Items stagger in with slide animation
Prevention
- Define variants separately for parent and children.
- Use initial and animate on the parent to propagate animation states.
- Use staggerChildren in parent variants to delay child animations.
- Children inherit the parent's animate state if they have matching variant names.
- Variants support transition properties like staggerChildren, delayChildren, and when.
Common Mistakes with framer motion variants
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
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