How to use Lottie Animation in a React App

Animations make any web app or mobile app more attractive and user-friendly. Animation can be done by code or we can use a lottie.

Why use a Lottie

A Lottie is saved in JSON format, taking much less space than GIF or video. lotties are scalable and can be used for different platforms- web, Android, or iOS.

Using Lottie

We can use any free lottie animations available. Download the animation in JSON format.

For this article, we’ll be using this lottie animation from lottiefiles.com :

  1. Create a new React project
[Terminal] npx create-react-app lottie-demo [Terminal]

2. Install the required package

[Terminal] npm i lottie-web [Terminal]

Download any lottie animation from lottiefiles.com.

3. Loading the animation

Open the app.js file and use loadAnimation() in useEffect() for loading animation. It returns the animation instance. It takes an object in which we can pass params :

  • container (required): HTML element that will contain the animation
  • renderer: select which renderer to use for the animation. 
    values are :
    1. SVG (default)
    2. canvas
    3. html
  • loop: true for an infinite number of loops, or we pass a number for a specific amount of loops (default is true)
  • autoPlay: the animation will play as soon as it’s loaded (default is true)
  • path or animationData (required at least one): pass a path to an external JSON file or pass a JSON object via animationData
[React] import React, { useEffect, useRef } from “react”; import lottie from “lottie-web”; import Lottie from “./transformer.json”; import “./App.css”; function App() { const lottieContainer = useRef(null); useEffect(() => { if (lottieContainer.current) { lottie.loadAnimation({ container: lottieContainer.current, renderer: “svg”, loop: true, autoplay: true, animationData: Lottie, }); } return () => { lottie.destroy(); }; }, []); return (

Lottie Demo

); } export default App; [React]

Run the app npm start

😃Hurray! Animation is played

Wait, there are methods through which we can control the animation such as:

  • play()
  • pause()
  • stop()
  • setSpeed(speed): 1 is the normal speed
  • destroy()

and more.

For implementing some of the above methods, the following code can be referred to:

[React] import React, { useEffect, useRef } from “react”; import lottie from “lottie-web”; import Lottie from “./transformer.json”; import “./App.css”; function App() { const lottieContainer = useRef(null); useEffect(() => { if (lottieContainer.current) { lottie.loadAnimation({ container: lottieContainer.current, renderer: “svg”, loop: true, autoplay: true, animationData: Lottie, }); } return () => { lottie.destroy(); }; }, []); const handleSpeedHalf = () => { lottie.setSpeed(0.5); }; const handleSpeedDouble = () => { lottie.setSpeed(2); }; const pauseLottie = () => { lottie.pause(); }; const destroyLottie = () => { lottie.stop(); }; const playLottie = () => { lottie.play(); }; return (

Lottie Demo

); } export default App; [React]

http://sh017.hostgator.tempwebhost.net/media/39b51a22529188f6170a231e12c8b9f0

Final Code

Hope this was useful for creating animation through Lottie. You can go through the final code at Code Sandbox.

Thank You
Keep Learning🤓

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

How to Determine Whether a Component Is Present in ViewPort

Hi guys, in this article we will learn about how to check the position of a component in the viewport, i.e., the viewable area...

Range Slider in React Native

There might be some scenarios such as show distance range or proficiency of skills in which we have to use a slider. So to...

Enhancing Performance and Optimization of React Native Applications

There are some ways to improve react-native app performance we will discusses one by one There are many approaches out there to avoid this issue. 1....

LEAVE A REPLY

Please enter your comment!
Please enter your name here