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.
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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
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 install --save lottie-react-native
npm install --save lottie-ios@3.4.0
This command will copy all the dependencies into your node_module directory.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
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
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
This is how you can use React Native Lottie Component for Android and iOS. If you have any doubts or 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. 🙂