Master React Native Reanimated Part 1

Steve Blue
4 min readJul 31, 2023

In React Native, Reanimated is a powerful animation library that allows you to create complex and high-performance animations. Reanimated extends the capabilities of React Native’s Animated API by enabling you to define animations and interactions directly in the native layer, which results in smoother and more efficient animations.

Before diving into learning React Native Reanimated, it’s beneficial to have a good understanding of the following prerequisites:

In React Native, the UI thread and the JavaScript (JS) thread are different and operate independently.

1. UI Thread (Main Thread):
The UI thread, also known as the main thread, is responsible for handling the user interface of your React Native app. This thread is where all UI rendering, layout calculations, and user interactions take place. When you update the state of a React component or trigger a UI event like a button press, these actions are processed on the UI thread.

However, any time-consuming operations executed on the UI thread can lead to a slow or unresponsive user interface. This is why it’s essential to avoid performing heavy computations or blocking tasks on the UI thread. Otherwise, the app might freeze or become sluggish, resulting in a poor user experience.

2. JavaScript (JS) Thread:
The JavaScript thread in React Native is where the majority of your application’s logic resides. It is responsible for executing the JavaScript code that makes up your React Native app. This includes handling state changes, updating component props, and performing calculations.

Animations and interactions created using the standard React Native Animated API also run on the JS thread. While the Animated API is efficient for simple animations, more complex and performant animations can be achieved using third-party libraries like Reanimated, which allows animations to run directly on the native side, separate from the JS thread.

To maintain a smooth and responsive user interface, it’s essential to keep the JS thread unblocked and free from heavy computations. This way, it can efficiently handle the application’s logic while offloading time-consuming operations to separate threads, such as using Reanimated worklets or scheduling tasks on the native side for better performance.

In summary, in React Native, the UI thread is responsible for the user interface, while the JS thread handles the application’s logic. Keeping these threads separate and ensuring that each performs its designated tasks efficiently is critical for a well-performing and user-friendly mobile app.

3. Worklet
In Reanimated, a worklet is a JavaScript function that can be executed on the native side of the application. Worklets are used to define animation and interaction logic that will be executed directly in the native runtime, bypassing the JavaScript thread. By doing so, worklets significantly improve the performance of animations as they do not rely on the bridge communication between JavaScript and the native code, which can be relatively slow.

Reanimated worklets are written in JavaScript but executed on the native side using a Just-in-Time (JIT) compilation. Worklets can be used to define animations, gestures, and other interactive behaviors more efficiently compared to using only JavaScript-based animations. It’s important to note that not all JavaScript functions can be executed as worklets, and there are certain limitations on what can be done within a worklet.

4. runOnUI
The runOnUI function in Reanimated is used to schedule a JavaScript function to be executed on the UI thread. While most of the Reanimated animations are executed on the native side, there might be situations where you need to execute some JavaScript code on the main thread. For example, updating the state of a React component, interacting with the UI elements, or performing certain UI-related operations that should not be executed on the native side.

By using runOnUI, you can ensure that certain UI operations are performed on the main thread without interfering with the high-performance animations running on the native side. It’s essential to use runOnUI judiciously, as executing too much code on the main thread can potentially impact the performance of your animations.

5. runOnJS
The runOnJS function in Reanimated is used to schedule a JavaScript function to be executed on the JavaScript thread. This is useful when you need to perform some JavaScript-related operations within the context of a Reanimated animation.

For instance, if you have a Reanimated animation that changes the opacity of an element based on certain conditions, you might want to perform additional JavaScript logic within that animation. By using runOnJS, you can ensure that the specified JavaScript function is executed in the JavaScript thread, allowing you to handle more complex logic while still leveraging the high-performance benefits of Reanimated for the animation itself.

In summary, Worklets enable you to run animation and interaction logic on the native side, while `runOnUI` and `runOnJS` provide the ability to schedule JavaScript functions to be executed on the main thread or the JavaScript thread, respectively, allowing for more flexibility in your animations and interactions.

--

--

Steve Blue

Experienced Mobile Application Developer with a demonstrated history of working in the computer software industry.