React Native Codegen Tools

0.70.7 · active · verified Sun Apr 19

react-native-codegen is an essential internal tool within the React Native ecosystem, responsible for generating native code (Java, Objective-C++, C++) from JavaScript type definitions (Flow or TypeScript). These generated definitions form the basis of "TurboModules" and "Fabric Components" in React Native's New Architecture, enabling type-safe, performant communication between JavaScript and native platforms. It primarily operates at build time, invoked by the React Native CLI, to automatically create the necessary bridging code for native modules and UI components. The package versioning is tightly coupled with the React Native monorepo, with the latest stable versions generally mirroring the `react-native` package's minor version (e.g., `react-native-codegen@0.85.x` for `react-native@0.85.x`). Its release cadence follows the React Native release schedule, typically bi-monthly or quarterly for minor versions. It differentiates itself from older bridging mechanisms by providing a standardized, strongly-typed, and automatically generated interface, significantly reducing boilerplate and improving developer experience and performance for native module authors.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a TurboModule spec file using TypeScript, which `react-native-codegen` processes to generate native code, and how to trigger the generation via the React Native CLI.

import type { TurboModule } from 'react-native';
import type { Int32 } from 'react-native/Libraries/Types/CodegenTypes';

// native_modules/MyNativeModule/NativeMyNativeModule.ts

/**
 * This is the interface for your native module, defining methods and constants.
 * The codegen tool reads this TypeScript (or Flow) spec file.
 */
export interface Spec extends TurboModule {
  /**
   * Say hello to the native side.
   * @returns A greeting string from native.
   */
  sayHello(): Promise<string>;
  /**
   * Adds two integers on the native side.
   * @param a The first integer.
   * @param b The second integer.
   * @returns The sum of a and b.
   */
  add(a: Int32, b: Int32): Int32;
  /**
   * Returns a constant object.
   */
  getConstants(): {
    DEFAULT_VALUE: string;
  };
}

// Declare the global TurboModule proxy for type safety
declare global {
  var __turboModuleProxy: (moduleName: string) => Spec | null;
}

// --- To generate native code, run this command in your project root ---
// npx react-native codegen-native-modules \ 
//   --platform ios \ 
//   --platform android \ 
//   --project-root . \ 
//   --output-dir ./node_modules/my-native-module/generated

// (Note: The --output-dir is illustrative; in a real project, this might be managed by the CLI)

view raw JSON →