loading...
Cover image for Lottie Animations for React

Lottie Animations for React

proiacm profile image CiaraMaria Updated on ・2 min read

Who doesn't love animations? They're fun, engaging, and versatile. In this post, I will introduce Lottie animations and how to implement them into your React website.

What is a Lottie?

A Lottie is a JSON-based animation file format that enables designers to ship animations on any platform. They are small files that work on any device and can scale up or down without pixelation.

Integrating Lottie into React App

Step 1: install the library

npm install --save react-lottie

Step 2: select an animation from LottieFiles

You can download it as a JSON or use the Lottie Animation URL.
Personally, I prefer to download and store it in a folder in my application.
Here's an example of the file structure:

Step 3: create a Lottie.js file in src

The code inside that file should look like this:

import React from "react";
import Lottie from "react-lottie";

export default function LottieAnimation({ lotti, width, height }) {
  const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: lotti,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice",
    },
  };

  return (
    <div>
      <Lottie options={defaultOptions} height={height} width={width} />
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode
Step 4: import your Lottie into the component you want to return the animation:
import React from 'react';
import LottieAnimation from '../Lottie';
import home from '../Animation/dev.json';

const Example = () => {

    return ( 
         <div className='example'> 
           <LottieAnimation lotti={home} height={300} width={300} />
        </div>
    )
}

export default Example; 
Enter fullscreen mode Exit fullscreen mode

Note: This example implies that you then add <Example /> into render in App.js


That's it! Your beautiful Lottie animation should now render to your site!

As always, feedback and discussion is welcome. 💁

Discussion

pic
Editor guide
Collapse
0916dhkim profile image
Donghyeon Kim

I've never heard of Lottie before, and it looks promising. To my understanding, Lottie is to GIF as SVG is to JPG.

Collapse
devhammed profile image
Hammed Oyedele

Exactly but it gives you more than what this post discussed.

You can play, pause, forward, backward the animations just like videos.

Collapse
teysworld profile image
Teysworld

This is awesome! I am new to React and loving these new discoveries

Collapse
jwhubert91 profile image
James Hubert

Excellent. This is right up my alley. Thanks for posting.

Collapse
proiacm profile image