React Native Reanimated and Worklets

repository·main·Indexed 25 days ago

https://github.com/software-mansion/react-native-reanimated

High-performance libraries by Software Mansion for creating smooth animations and enabling multi-threaded JavaScript execution in React Native. This repository includes React Native Reanimated (current major version 4) and React Native Worklets, providing tools for parallel execution without requiring native code.

Tokens
123.4K
Snippets
329
Records
713
Agent score
94%

What's inside react-native-reanimated

  1. Overview of Reanimated and Worklets

    main

    This repository contains two primary libraries designed for high-performance React Native applications:

    • React Native Reanimated: A library for creating smooth animations and interactions.
    • React Native Worklets: A library that enables multi-threaded JavaScript execution in React Native applications.

    Reanimated 4 is the current major version.

  2. Understand React Native (RN) Runtime vs Worklet Runtimes

    main

    The Worklets library operates using two primary types of runtimes: the React Native Runtime and Worklet Runtimes.

    • RN Runtime: The standard JavaScript runtime spawned by React Native (the JS Thread). It is the only runtime with access to React Native APIs, app state, and components. There is exactly one RN Runtime per app.
    • Worklet Runtime: A specialized JavaScript runtime spawned by the Worklets library, pre-configured to execute worklets. It does not share memory with the RN Runtime but communicates via specific APIs. Each Worklet Runtime is identified by a unique runtimeId used for communication or creating Shareables.
  3. Understand Workletization and the Worklets Babel plugin

    main
    Workletization is the process of converting a JavaScript function into a serializable object that can be copied and run on Worklet Runtimes. The Worklets Babel plugin automates this by picking up functions marked with the 'worklet' directive or functions used within Reanimated APIs (like useAnimatedStyle), reducing the need for manual boilerplate.
  4. Legacy Eval Mode vs Bundle Mode

    main

    Worklets can operate in two modes:

    1. Bundle Mode: The recommended mode where worklets have access to the full JS bundle and pre-compiled bytecode optimizations.
    2. Legacy Eval Mode: The former method where each worklet is serialized as a string and evaluated individually on the Worklet Runtime. This is currently the default mode, but it is strongly recommended to opt out of it in favor of Bundle Mode.
  5. Understand the Reanimated monorepo structure

    main

    The repository is organized as a monorepo. Key directories include:

    • apps/: Contains example applications (e.g., fabric-example for New Architecture, web-example for Web, macos-example, next-example, tvos-example, and common-app for shared source).
    • packages/: Contains the core libraries:
      • react-native-reanimated: The main package, including android (native), apple (iOS native), Common (shared C++), scripts (CI), and src (JS source).
      • react-native-worklets: The worklets package.
      • eslint-plugin-reanimated: The ESLint plugin.
    • docs/: Contains documentation for Reanimated (docs-reanimated) and Worklets (docs-worklets).
  6. Understand Serializable shared memory

    main

    Serializable is a shared memory type used to hold immutable values that can be serialized and deserialized across different JavaScript Runtimes. It enables passing JavaScript values between Runtimes by ensuring data is correctly transferred and reconstructed.

    Important constraints:

    • The reference is a wrapper and cannot be manipulated directly as it does not represent a standard JavaScript object.
    • You cannot pass JavaScript values to other Runtimes without prior serialization.
    • Functions that are not worklets are serialized as references to function instances on their respective Runtime; they can be passed around but cannot be invoked on other runtimes.
  7. Understand Worklets in Reanimated

    main

    Worklets are functions that can be executed on both the JavaScript thread and the UI thread. This allows Reanimated to perform smooth animations by running logic directly on the UI thread, avoiding expensive communication between threads.

    A worklet is defined as an Arrow Function Expression, Function Declaration, Function Expression, or Object Method that contains a 'worklet' directive at the very top of its body.

    const foo = () => {
      'worklet';
      console.log('Hello from the UI thread');
    }
  8. Distinguish between the JavaScript thread and the UI thread

    main

    Understanding the threading model is crucial for performance:

    • JavaScript thread (JS thread): The primary environment for the React Native Runtime. It handles business logic, state management, and JavaScript event handling.
    • UI thread (Main thread): Responsible for handling user interface updates. Worklets are often used to move high-frequency logic from the JS thread to this thread to ensure smooth animations.
  9. Understand Worklets memory sharing primitives

    main

    Because Worklets run JavaScript on multiple threads (Runtimes), they cannot share the same JavaScript heap. To share data between Runtimes, Worklets use three distinct memory primitives:

    1. Serializable: A copy-only model. Data is serialized into a byte stream and reconstructed in the target Runtime. Changes made in one Runtime are not visible in others.
    2. Synchronizable: An unbound, shared model. The actual value lives in C++ (outside any Runtime) and is guarded by mutexes. All Runtimes see the same value, but every read/write requires a costly round-trip through C++.
    3. Shareable: A bound, asymmetric model. The value lives as a plain JavaScript object on a designated Host Runtime. Guest Runtimes hold a reference to it. This is the most efficient for non-trivial data if one Runtime dominates access, but Guest Runtimes must incur a round-trip to the Host to access it.

    Use the following guide to choose the right primitive for your use case.