React Animation with Framer Motion: The Ultimate Guide

Adding animations to your web applications can significantly enhance the user experience by making your application more dynamic and interactive. This comprehensive guide will give you a practical understanding of implementing animations in your React applications using Framer Motion. With its powerful features and simple API, Framer Motion is the perfect choice for creating smooth animations in your React projects.

Introduction to React Animation with Framer Motion

In today's digital landscape, user experience (UX) has become a central focus of web development. Providing a smooth, captivating, and aesthetically pleasing interface can really influence user satisfaction and retention. Animations are crucial in enhancing UX by making your applications more visually appealing and interactive.

Framer Motion is a powerful and user-friendly animation library specifically designed for React applications. It enables you to create intricate animations with minimal code, thanks to its simple and expressive API. In this guide, we will explore how to use Framer Motion to implement various types of animations in your React applications.

What Is Framer Motion?

Framer Motion is an open-source, production-ready animation library for React that offers a simple and expressive API for creating animations. It provides a wide range of animation capabilities, including spring physics, gesture handling, and server-side rendering support. This makes Framer Motion an ideal choice for implementing animations in React applications.

Setting Up Your React Project with Framer Motion

To get started with Framer Motion, you need to set up a new React project and install the necessary dependencies. First, create a new React project using the following command:

yarn create react-app react-framer-animation

Next, navigate to your React project directory and install the Framer Motion package:

yarn add framer-motion

This command will add the Framer Motion dependency to your React application, allowing you to start implementing animations.

Understanding Motion Components

Motion components are the building blocks of Framer Motion animations. They are created by prefixing motion to your regular HTML and SVG elements (e.g., motion.div, motion.h1). Motion components can accept several props, with the most basic one being the animate prop. This prop takes in an object where you define the properties of the component that you want to animate.

For example, to create a simple animation where an h1 element slides to the right and moves up, you can use the following code:

import { motion } from 'framer-motion';

<motion.h1 animate={{ x: 20, y: -20 }}>

  This is a motion component


Working with the Animate Prop

The animate prop allows you to define the final state of the animation. Framer Motion will automatically infer the initial state based on the specified CSS properties or their default values. For instance, if you want to animate the opacity of an element, Framer Motion will know that the default opacity value is 1, and animate it accordingly.

You can also set the initial values of animatable CSS properties using the initial prop, which accepts an object containing the properties and their initial values. This enables you to create animations that transition from a custom initial state to the final state defined by the animate prop.

import { motion } from 'framer-motion';

<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }}>

  Fade-in animation


Configuring Transitions

Transitions in Framer Motion are used to define the timing and type of animation for a motion component. By default, Framer Motion will create an appropriate animation for a snappy transition based on the types of values being animated. However, you can customize the animation by passing a transition prop to the motion component.

The transition prop accepts an object with properties such as duration, ease, and type. These properties allow you to control the speed, easing function, and animation type for the transition.

import { motion } from 'framer-motion';


  animate={{ x: 100 }}

  transition={{ duration: 2, ease: "easeOut" }}


  Custom transition


Creating Enter and Exit Animations

Framer Motion makes it easy to create enter and exit animations for your motion components. When a motion component is first created, it will automatically animate to the values in the animate prop if they are different from those defined in the style or initial prop. You can set the initial prop to false to disable enter animations.

To create exit animations, you can use the AnimatePresence component provided by Framer Motion. This component keeps motion components in the DOM while they perform exit animations, ensuring a smooth transition when components are removed from the tree.

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


  {isVisible && (


      initial={{ opacity: 0 }}

      animate={{ opacity: 1 }}

      exit={{ opacity: 0 }}


      Enter and exit animation




Using Variants for Complex Animations

Variants are a powerful feature of Framer Motion that allows you to create complex and orchestrated animations in a declarative manner. Variants are sets of pre-defined animation targets, which are passed into motion components via the variants prop.

You can refer to these variants by label, making it easy to create animations that propagate throughout the DOM and orchestrate their execution.

import { motion } from 'framer-motion';

const variants = {

  hidden: { opacity: 0 },

  visible: { opacity: 1 },


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

  Variants example


Gesture-Triggered Animations

Framer Motion allows you to create animations that respond to user gestures, such as hover, tap, drag, focus, and inView. These animations can be defined using props like whileHover, whileTap, and whileInView.

For example, to create a button that scales up when hovered and scales down when tapped, you can use the following code:

import { motion } from 'framer-motion';


  initial={{ opacity: 0.6 }}

  whileHover={{ scale: 1.2, transition: { duration: 1 } }}

  whileTap={{ scale: 0.9 }}

  whileInView={{ opacity: 1 }}


  Gesture-triggered animation


Implementing Route Animations

Animating routes can make transitions between different pages or sections of your React application more visually appealing and interactive. Framer Motion can be used in conjunction with React Router to create smooth route animations.

To implement route animations, you need to wrap your application's routes with the AnimatePresence component and use the motion components to define the animations for each route.

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

import { Switch, Route, useLocation } from 'react-router-dom';

const location = useLocation();

<AnimatePresence exitBeforeEnter>

  <Switch location={location} key={location.key}>

    <Route path="/page1">

      <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }}>

        Page 1



    <Route path="/page2">

      <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }}>

        Page 2






Framer Motion is a powerful and easy-to-use animation library for React applications. With its simple API and extensive animation capabilities, you can create intricate animations to enhance your application's user experience. This comprehensive guide has introduced you to the basics of Framer Motion and how to implement various types of animations in your React applications. Now it's time to unleash your creativity and start animating your own projects with Framer Motion.