React Native - Getting Started with Lottie and Expo

Published on Mar 9, 2018

5 min read

EXPO

In this tutorial, we will be using Lottie with React Native. Lottie is an opensource library that renders Adobe Effects by providing easy to use animations just like static images. These animations are beautiful. Lottie is a mobile library developed by AirBnB. These animations are exported as JSON files using Bodymovin to render the natively on mobile and in web applications.

👍 for designers who spend their time contributing in LottieFiles.

Requirements

🔗

I will be using Expo XDE for brevity and to get started quickly. Create a new project using it. I am going to call rn-lottie-example but you can call whatever you want.

When the XDE completes running ReactNative Package Manager and builds up the project, you can go to Device dropdown menu at the right and click on whichever simulator you have installed depending on the operations system you are using. Windows users please make sure you have android studio and necessary files installed and MacOS users, please have XCode installed or up to date.

You can also view the application, by running it using Expo Client on your mobile device. Note that, depending on your internet connection, this can be slow and your mobile device and development machine must be on same wifi. Scan the QR code in Share section, you are ready to go. Once, the app is rendered, you will be welcomed by the default screen:

Now open your favorite text editor/IDE because we are going to start writing code in next section.

Getting Started

🔗

Installing Lottie as a dependency in a project can a bit tricky but fortunately, Expo provides support for it. We do not have to install anything since we are using Expo. We directly import the dependency in our App.js:

1import { DangerZone } from 'expo';
2const { Lottie } = DangerZone;

Sine Lottie in Expo project is in Alpha mode, do not get worried by the word DangerZone.

Include a local state in our application called animation. We will call this help in playing and restarting the animation itself. I am using this file for our animation from LottieFiles.com so you can download it. Of course, you are free to choose any other. Animations listed on the site are open source.

1state = {
2 animation: null
3};

Later, we will using the state.animation to source the animation file fetched directly in Lottie component.

Defining the animation

🔗

We will define two custom functions: _playAnimation and _loadAnimationAsync that perform the animation and load the animation from the internet using the fetch API. We will also be pre-mounting our animation using componentWillMount() method available to us by core React. In this Life cycle method, when the state is set, it can be called before the initial render.

In general, it is used to prepare either the first render or update the state before the render. This is why we are using it. We need to update the state we defined.

1componentWillMount() {
2 this._playAnimation();
3 }
4
5 _playAnimation = () => {
6 if (!this.state.animation) {
7 this._loadAnimationAsync();
8 } else {
9 this.animation.reset();
10 this.animation.play();
11 }
12 };
13
14 _loadAnimationAsync = async () => {
15 let result = await fetch(
16 'https://www.lottiefiles.com/storage/datafiles/a795e9d1bd5672fd901329d51661db5c/JSON/location.json'
17 );
18
19 this.setState(
20 { animation: JSON.parse(result._bodyText) },
21 this._playAnimation
22 );
23 };

Inside, _loadAnimationAsync() we use JavaScript's fetch API to get the animation from its source. This does mean, that the animation is coming from internet so if you are testing on your mobile device, make sure you have access to internet.

This how our render function looks like:

1render() {
2 return (
3 <View style={styles.container}>
4 <View style={styles.animationContainer}>
5 {this.state.animation && (
6 <Lottie
7 ref={animation => {
8 this.animation = animation;
9 }}
10 style={styles.loadingAnimation}
11 source={this.state.animation}
12 />
13 )}
14 </View>
15 </View>
16 );
17 }

We separately define styling for the overall container and the animation. To load the animation, we will set its backgroundColor to transparent in order to remove any background color it has by default.

1const styles = StyleSheet.create({
2 container: {
3 flex: 1,
4 backgroundColor: '#fff',
5 alignItems: 'center',
6 justifyContent: 'center'
7 },
8 animationContainer: {
9 backgroundColor: '#fff',
10 alignItems: 'center',
11 justifyContent: 'center',
12 flex: 1
13 },
14 loadingAnimation: {
15 width: 400,
16 height: 400,
17 backgroundColor: 'transparent'
18 }
19});

You can see the application working

This tutorial shows how you can get started with Lottie animation library using React Native and Expo. There are other ways to define animations and you can go in-depth as much as you want. This article is written for a beginner’s point of view.

For alternative to Lottie, you can check Facebook’s Keyframes that also uses Adobe After Effects to create one.

Complete code of our demo application:

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
3import { DangerZone } from 'expo';
4const { Lottie } = DangerZone;
5
6export default class App extends React.Component {
7 state = {
8 animation: null
9 };
10
11 componentWillMount() {
12 this._playAnimation();
13 }
14
15 _playAnimation = () => {
16 if (!this.state.animation) {
17 this._loadAnimationAsync();
18 } else {
19 this.animation.reset();
20 this.animation.play();
21 }
22 };
23
24 _loadAnimationAsync = async () => {
25 let result = await fetch(
26 'https://www.lottiefiles.com/storage/datafiles/a795e9d1bd5672fd901329d51661db5c/JSON/location.json'
27 );
28
29 this.setState(
30 { animation: JSON.parse(result._bodyText) },
31 this._playAnimation
32 );
33 };
34
35 render() {
36 return (
37 <View style={styles.container}>
38 <View style={styles.animationContainer}>
39 {this.state.animation && (
40 <Lottie
41 ref={animation => {
42 this.animation = animation;
43 }}
44 style={styles.loadingAnimation}
45 source={this.state.animation}
46 />
47 )}
48 </View>
49 </View>
50 );
51 }
52}
53
54const styles = StyleSheet.create({
55 container: {
56 flex: 1,
57 backgroundColor: '#fff',
58 alignItems: 'center',
59 justifyContent: 'center'
60 },
61 animationContainer: {
62 backgroundColor: '#fff',
63 alignItems: 'center',
64 justifyContent: 'center',
65 flex: 1
66 },
67 loadingAnimation: {
68 width: 400,
69 height: 400,
70 backgroundColor: 'transparent'
71 }
72});

You can also find the complete code at this Github repo, in case you just want to play around this concept.

Originally this article was published on Hackernoon.com


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.


Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.