Course contentsShow
← React
Lesson 1973 of 2,39645. AnimationsFree lesson

Variants: Named Animation States

Define reusable animation configurations as variants objects for cleaner code.

Variants: Named Animation States

You'll learn how to clean up repetitive animation code by defining reusable animation configurations as named variants objects.

The Problem with Inline Animation Props

When you have multiple related animation states (hover, tap, loading, error), passing animation props directly to motion components gets verbose and repetitive fast. You end up duplicating values across components or copying animation objects everywhere.

What Variants Are

Variants are named animation configurations that you define once as an object, then reference by name in your motion components. Think of them like CSS classes for animations—you define the "states" once, then apply them declaratively.

Instead of:

<motion.div 
  initial={{ opacity: 0, scale: 0.8 }}
  animate={{ opacity: 1, scale: 1 }}
/>

You define variants:

const variants = {
  hidden: { opacity: 0, scale: 0.8 },
  visible: { opacity: 1, scale: 1 }
};

Then reference them by name:

<motion.div 
  variants={variants}
  initial="hidden"
  animate="visible"
/>

Why This Matters

Code reusability: Define once, use everywhere. Change one variants object to update all instances.

Semantic clarity: Names like "loading", "success", or "error" communicate intent better than raw property values.

Propagation to children: Framer Motion automatically propagates variant names down the component tree—child motion elements can respond to parent state changes without prop drilling.

Easier state management: Toggle between animation states by just changing the string you pass to animate.

Key Takeaway: Variants let you define reusable, named animation states that make your animation code cleaner, more maintainable, and semantically clearer—essential for scaling animations across a real application.