Menu

Getting Started with Framer Motion: Animate Your React Apps with Ease

Learn how to use Framer Motion to add smooth, engaging animations to your React applications. This comprehensive guide covers installation, basic usage, advanced techniques, and practical examples.

Nikhil Singh

5 min read
Getting Started with Framer Motion: Animate Your React Apps with Ease

Framer Motion has revolutionized the way developers approach animations in React applications. In this comprehensive guide, we'll explore how to use Framer Motion to create fluid, interactive animations that will elevate your React projects to the next level.

What is Framer Motion?

Framer Motion is a production-ready animation library for React that combines power with ease of use. It provides a simple declarative syntax for creating complex animations, making it accessible for beginners while offering advanced features for experienced developers.

Why Choose Framer Motion for React Animations?

  • Intuitive API: Framer Motion's declarative approach makes it easy to create and maintain animations.
  • Powerful Features: From basic transitions to complex gestural interactions, Framer Motion can handle it all.
  • Performance-Focused: Built with efficiency in mind, ensuring smooth animations even on lower-end devices.
  • React-Friendly: Seamlessly integrates with React's component model and lifecycle.

Installation

To get started with Framer Motion, follow these simple steps:

npm install framer-motion

Or if you're using Yarn:

yarn add framer-motion

Basic Usage

Let's start with a simple fade-in animation to get familiar with Framer Motion's syntax:

import React from 'react';
import { motion } from 'framer-motion';

function FadeInComponent() {
  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      transition={{ duration: 1 }}
    >
      This text will fade in!
    </motion.div>
  );
}

export default FadeInComponent;

Advanced Techniques

Using Variants

Variants allow you to define reusable animation states and orchestrate complex sequences:

import React from 'react';
import { motion } from 'framer-motion';

const boxVariants = {
  hidden: { opacity: 0, scale: 0.8 },
  visible: { 
    opacity: 1, 
    scale: 1,
    transition: {
      delayChildren: 0.3,
      staggerChildren: 0.2
    }
  }
};

const itemVariants = {
  hidden: { y: 20, opacity: 0 },
  visible: {
    y: 0,
    opacity: 1
  }
};

function VariantsExample() {
  return (
    <motion.div
      variants={boxVariants}
      initial="hidden"
      animate="visible"
    >
      {[1, 2, 3].map(index => (
        <motion.li key={index} variants={itemVariants}>
          Item {index}
        </motion.li>
      ))}
    </motion.div>
  );
}

export default VariantsExample;

Gesture Animations

Framer Motion makes it easy to add interactivity to your animations:

import React from 'react';
import { motion } from 'framer-motion';

function DraggableComponent() {
  return (
    <motion.div
      drag
      dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
      whileHover={{ scale: 1.1 }}
      whileTap={{ scale: 0.9 }}
    >
      Drag me!
    </motion.div>
  );
}

export default DraggableComponent;

Optimizing Performance

While Framer Motion is designed to be performant out of the box, here are some tips to ensure your animations run smoothly:

  1. Use the layout prop for automatic animations: This can significantly reduce the amount of code you need to write for layout changes.

  2. Leverage the useReducedMotion hook: This ensures your animations respect the user's system preferences for reduced motion.

  3. Use the willChange property: This hints to the browser about which properties will animate, allowing for optimizations.

import React from 'react';
import { motion, useReducedMotion } from 'framer-motion';

function OptimizedAnimation() {
  const shouldReduceMotion = useReducedMotion();

  return (
    <motion.div
      layout
      style={{ willChange: 'transform' }}
      animate={{ x: shouldReduceMotion ? 0 : 100 }}
    >
      Optimized animation
    </motion.div>
  );
}

export default OptimizedAnimation;

Real-World Example: Animated Modal

Let's look at a practical example of how you might use Framer Motion in a real application:

import React from 'react';
import { motion, AnimatePresence } from 'framer-motion';

function AnimatedModal({ isOpen, onClose, children }) {
  return (
    <AnimatePresence>
      {isOpen && (
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          onClick={onClose}
          style={{
            position: 'fixed',
            top: 0,
            left: 0,
            width: '100%',
            height: '100%',
            background: 'rgba(0, 0, 0, 0.5)',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center'
          }}
        >
          <motion.div
            initial={{ y: -50, opacity: 0 }}
            animate={{ y: 0, opacity: 1 }}
            exit={{ y: 50, opacity: 0 }}
            onClick={e => e.stopPropagation()}
            style={{
              background: 'white',
              padding: '20px',
              borderRadius: '5px'
            }}
          >
            {children}
          </motion.div>
        </motion.div>
      )}
    </AnimatePresence>
  );
}

export default AnimatedModal;

Troubleshooting Common Issues

When working with Framer Motion, you might encounter some common issues. Here's how to address them:

  1. Animations not working: Ensure you're using the motion component and that your animate prop is correctly defined.

  2. Choppy animations: Try adjusting the transition property or use the useReducedMotion hook to respect user preferences.

  3. Unexpected behavior with nested animations: Use the propagate prop to control how animations propagate to child elements.

Conclusion

Framer Motion offers a powerful yet intuitive way to add animations to your React applications. By leveraging its declarative API and advanced features, you can create engaging user experiences that set your projects apart.

Remember, the key to mastering Framer Motion is practice and experimentation. Start with simple animations and gradually incorporate more complex techniques as you become comfortable with the library.

As you continue your journey with Framer Motion, explore the official documentation for in-depth information on all features, join the community to share your creations, and don't hesitate to push the boundaries of what's possible with animations in React.

Happy animating!

Level Up Your UI for FREE

An open-source collection of beautiful animated components

Explore Components