React Native Lottie Component for Android and iOS

React Native Lottie

This is an example of a React Native Lottie Component for Android and iOS. Lottie is a mobile library for Android and iOS that parses Adobe After Effects animations exported as JSON and renders them natively on mobile.

Nowadays application development is not only about making a functional mobile apps, but also about making it attractive for users is equally important. If you see the mobile applications of Uber, Airbnb, CRED etc you will notice very beautiful animations to introduce the feature, making these kind of animations in mobile development without any external tool is not possible and components like Lottie can help you to integrate this animations in app.

With the help of any designers you can create and ship beautiful animations in your mobile app. Lottie libraries and plugins available for Web, iOS, Android, Flutter, ReactJS, React Native, Xamarin, NativeScript, Windows, Vue, Angular, QT, Skia, Framer X, Sketch, Figma & After Effects.

If you do not have any designer friend or any designer skill then you can take the help of lottiefiles.com, there are many animations freely available to integrate into your mobile app.

Lottie Animation

All of these animations were created in After Effects, exported with Bodymovin, and rendered natively with no additional engineering effort.

Bodymovin is an After Effects plugin created by Hernan Torrisi that exports After effects files as JSON and includes a javascript web player. Lottie is built on top of his great work to extend its usage to Android, iOS, React Native, and Windows.

How to Integrate Lottie Component in React Native

To use After Effects in mobile application we can use LottieView which can be easily imported and can be used in very simple way, just import LottieView

import LottieView from 'lottie-react-native';

and use it by passing animation.json

<LottieView
  ref={(animation) => {
    animationRef = animation;
  }}
  source={require('./animation.json')}
  autoPlay
  loop
/>

Lottie Example Description

In this example we will use an animation file with name animation.json which is downloaded from lottiefiles.com and will integrate using LottieView component.

Let’s see how to do it.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use LottieView we need to install lottie-react-native dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following commands to install

npm i --save lottie-react-native
npm i --save lottie-ios@3.1.8

This command will copy all the dependency into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

cd ios && pod install && cd ..

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

// React Native Lottie Component for Android and iOS
// https://aboutreact.com/react-native-lottie/

// import React in our code
import React, {useRef, useEffect} from 'react';

// import all the components we are going to use
import {SafeAreaView, StyleSheet, View, Text} from 'react-native';
import LottieView from 'lottie-react-native';

const App = () => {
  let animationRef = useRef();
  useEffect(() => {
    // To play complete animation
    animationRef.play();
    // Similary you can use this reset, pause, resume

    // To play from a specific startFrame and endFrame
    // animationRef.play(30, 120);
  }, []);

  return (
    <SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
      <View style={styles.container}>
        <Text style={styles.header}>
          React Native Lottie Component for Android and iOS
        </Text>
        <LottieView
          ref={(animation) => {
            animationRef = animation;
          }}
          source={require('./animation.json')}
          autoPlay
          loop
        />
      </View>
      <Text style={styles.smallText}>www.aboutreact.com</Text>
    </SafeAreaView>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 16,
  },
  header: {
    fontSize: 24,
    textAlign: 'center',
    fontWeight: 'bold',
  },
  smallText: {
    fontSize: 18,
    textAlign: 'center',
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

react_native_lootie1 react_native_lootie2 react_native_lootie3 react_native_lootie4

This is how you can use React Native Lottie Component for Android and iOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.