Augmented Reality
IT & Services
Blog

Creating Stellar Animations in React Native Using Reanimated 2.0

by
Lokeswari Satyanarayana
Co-author(s):
Varun Mehta
November 26, 2021
4
Min(s)

We live in a world of constant change. It’s not just that the technologies are constantly changing, but our behaviors and expectations are changing too. Every day there is something new to learn or some new skill to master, especially when it comes to software development. One area where developers are constantly learning is how to animate their apps better.

Animations play a significant role in providing visual cues and feedback to users, thus enhancing the product’s overall experience. Be it a visually pleasing loading indicator, a crisp transition between screens, or an eye-catching confetti pop-up. This is why animation is considered to be an essential aspect of interaction design. It's what makes users feel like they're interacting with a product that has life and personality.

Lack of animations or too many of them can cause frustration to end-users, thus affecting the overall product experience.

Let’s understand animations in-depth before diving into React Native animations and Reanimated 2.0.

What are Animations?

Animations can be defined as a series of frames put together. Each frame consists of a stationary image and time. When these frames are synchronized, they create an animation on the screen. 

One key aspect of animations is frames per second (FPS), a performance metric. The human brain perceives anything above 12 frames as an animation. A high FPS indicates smooth animations, whereas a low FPS implies glitchy and poor-quality animations. 

So, the necessary detail is to remember that animations must have a high FPS in mobile applications. So, let’s take a look at how to achieve that in React Native.

Animations in React Native

But first, you may be wondering about React Native. It is a JavaScript framework that is used to write native mobile applications for Android and iOS. React Native is also responsible for building user interfaces that target mobile platforms.

The secret to smooth animations in React Native is to avoid executing any animation-related code on the JavaScript thread. This is because the thread needs to be animation-free since the frame change has to be instantaneous.

However, the JavaScript thread might not always be free since it could be executing other functions simultaneously. This would result in lower FPS, resulting in buggy animations.

Higher FPS can be achieved with the help of the UI thread. This employs native components provided by Android/iOS to execute every frame when useNativeDriver is enabled. This combination helps in ensuring that higher FPS is maintained while eliminating frame drops.

Even though React Native is shipped with an animated Application Programming Interface (API), there are several downsides to it:

  • It runs on the JavaScript thread by default.
  • Despite allowing the utilization of the UI thread for animations, the Boolean only supports non-layout property animations.
  • Gesture-based animations can be challenging.

Hence, the alternative is to use Reanimated for animations. It is a React Native package that runs on the UI thread and helps developers achieve the ideal 60 FPS rate. 

Using Reanimated for Animations in React Native

One of the most significant advantages of using Reanimated with React Native is that gesture-based animations can be easily handled. Reanimated can be segregated into Reanimated 1.0 and 2.0

Reanimated 1.0 worked on nodes, which allowed each node to perform a single-step animation. Unfortunately, it had a steep learning curve with a different code from the rest of the application, making it less developer-friendly.

Hence, it made sense to release a developer-friendly and easy-to-learn version, leading to the launch of Reanimated 2.0.

Reanimated 2.0 makes use of Worklets, which are JavaScript code that can be executed on UI threads synchronously. This upgraded version of Reanimated uses a reactive style, shared values and hooks, and offers easy-to-use wrappers and modifiers for creating flawless animations. 

Furthermore, Reanimated isolates animation-related codes from the JavaScript thread, which eliminates the possibility of frame drops. In such cases, the JavaScript thread might be running detailed and extensive tasks, and the animations would still render seamlessly without any frame drops.

Reanimated 2.0 comes equipped with Layout Animations that help in adding/removing views, updating data, and assisting in showcasing visual updates with animation. In addition, it has built-in predefined styles for Layout Animations, and developers also have the option to build custom animation styles. However, it is important to note that the Layout Animation API is currently in the alpha version. 

Build Beautiful Animations With Mutual Mobile

As we can see, Reanimated 2.0 is not only easy to use and implement, but also enhances the quality of the animations. Additionally, it also improves the end-user and developer experience by a long margin.

So, if you are looking to build and implement animations for your mobile app, Mutual Mobile’s team of brilliant developers is here to guide you throughout the journey. Just reach out to us with your idea, and we will handle the rest!

Animate to Create Memorable Experiences

Talk to us