Getting ready with Framer Motion for React

Milind MishraMilind Mishra
3 min read

Lets dive into the world of animation in React using Motion for React (previously Framer Motion). Here are the practical insights, code snippets, and a few lessons learned along the way. If you want your UIs to truly come alive, use framer motion to add those tastefull animations!

1. Getting Started: Animating Your First Component

The motion component is the backbone basically, you wrap any element and it gains superpowers. For example:

// Animate a div scaling up
<motion.div animate={{ scale: 2 }} />

Changing props triggers graceful animations. By default, Framer Motion uses intuitive transitions, but you can customize them.

2. Customizing Transitions: Tween vs Spring

  • Tween: Basic, uses bezier curves.

      transition={{ type: "tween", ease: "easeInOut" }}
    
  • Spring: Fancier, feels more “alive.” You control stiffness/bounciness and damping/friction:

      transition={{ type: "spring", stiffness: 200, damping: 255 }}
    
    • Play with these numbers to experiment with "bounciness" or how quickly things settle.

    • Watch out for “overshooting” (when the animation goes past its target and snaps back). This means you may want to tweak these values!

  • Physics gotcha: With springs, you don’t set a fixed duration. The animation’s end is determined by physical properties, not a time value.

    • Rest Delta: If set too high, your animation can cut off early! Consider lowering to something like 0.01 for smooth results.

3. Initial Animation on Mount

Framer Motion auto-animates components “in” when they mount:

<motion.button initial={{ scale: 0 }} animate={{ scale: 1 }} />

If you want to disable that (so your component just appears), use:

<motion.button initial={false} animate={{ scale: 1 }} />

This trick is handy for controlling exactly when things animate.

4. Layout Animations: Beyond Simple Transitions

Framer Motion’s layout prop animates changes in size and position, performing “magic” with CSS transforms and JavaScript. Just add:

<motion.div layout />
  • The engine manages complex changes, transforming between shapes or positions as your React tree changes.

  • Motion’s animation engine uses techniques like FLIP (First-Last-Invert-Play) to animate between “before” and “after” states.

Parent ↔️ Child Sync

If both a parent and child element use the same transition settings, their animations will likely sync up well.

5. Working with Unique layoutId and LayoutGroup

  • layoutId: Needed when you want to animate specific elements between different parts of the render tree.

    • Make sure it’s globally unique! If you render multiple components with the same layoutId, their animations will get tangled.

    • Use:

        const id = React.useId();
        <motion.div layoutId={id} />
      
  • LayoutGroup: Group several components together so layouts and animations interact correctly:jsx

      import { LayoutGroup } from "framer-motion"
    
      <LayoutGroup>{/* motion components */}</LayoutGroup>
    

    Tells Framer Motion these components are part of the same animation universe.

6. Handy Animation Props & Gotchas

  • Border Radius & Animation: Sometimes animating borderRadius can create visual glitches. A fix is to always add animate and set initial as false.

      animate={{ borderRadius: 12 }} initial={false}
    
      // same as
      // setting properties to initial itself will achieve the same effect
      initial={{ borderRadius: 12 }}
    
  • Performance: Cancelling out (disabling) certain animations can be heavy on the client so use them with care.

7. A Few Final Insights

  • Everything in Framer Motion translates into CSS transforms under the hood, providing smooth performance.

  • You can animate size, position, and even custom styles and transforms.

  • The official Motion for React docs are an excellent next read, as they cover advanced topics, gestures, scroll animations, exit animations, and more.

Conclusion

Framer Motion opens a new dimension for React animation, striking a balance between usability and expressive power. My main advice: play with the numbers, understand the physics, read the docs, and don’t be afraid of a few “gotchas” they help you understand the underlying foundations which comes as an opportunity to learn from the best!

0
Subscribe to my newsletter

Read articles from Milind Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Milind Mishra
Milind Mishra

I'm a Developer from Bangalore, India, and I've recently been falling in love with Design and Development. I really enjoy learning languages and Web Development frameworks like React.