Member-only story

Add Animation to Our React App with the Framer Motion Library

John Au-Yeung
3 min readFeb 10, 2021

--

Photo by Rolands Zilvinskis on Unsplash

With the Framer Motion library, we can render animations in our React app easily.

In this article, we’ll take a look at how to get started with Framer Motion.

Installation

We can install Framer Motion by running:

npm install framer-motion

Getting Started

We can create a div that moves by writing”

import React from "react";
import { motion } from "framer-motion";
export default function App() {
return (
<div className="App">
<motion.div
animate={{ x: 100 }}
style={{ width: 100, height: 100, backgroundColor: "red" }}
/>
</div>
);
}

We set the animate prop to an object with the x property to move it horizontally.

Draggable Element

We can make an element draggable by using the drag and dragConstraints props.

To use them, we write:

import React from "react";
import { motion } from "framer-motion";
export default function App() {
return (
<div className="App">
<motion.div
drag="x"
dragConstraints={{ left: -100, right: 100…

--

--

No responses yet