Files
nitro/README.md
T
2024-07-02 19:25:58 +02:00

3.4 KiB

react-native-nitro

Nitro Modules are highly efficient native modules exposed with a statically compiled binding layer to JSI.

TODOs

  • Create a TypeScript AST-parser code-generator (TypeScript interfaces -> C++/Swift/Kotlin interfaces)
  • Bridge HybridObject and Modules to Swift/Kotlin
  • Implement proper autolinking so third party modules can use NitroModules and be automatically loaded in the end-app (android/build.gradle how??)
  • Implement Views/RawProps

Installation

  1. Install react-native-nitro-modules from npm:
yarn add react-native-nitro-modules
cd ios && pod install
  1. If you don't already have one, add an empty Swift file to your iOS project (including a generated bridging header).
    1. Open your .xcworkspace in Xcode
    2. Right-click on your app's project on the left
    3. New File > Swift File > Call it DummyFile.swift > Create
    4. When promted to create a Bridging Header, press Create Bridging Header

Usage

1. Create a TypeScript spec

The TypeScript spec is the single source of truth. It's interfaces, enums or other type declarations will be converted to C++ (or Swift/Kotlin) types using a code generator.

export interface MathsModule {
  readonly pi: number
  multiply: (a: number, b: number) => number
  subtract: (a: number, b: number) => number
  add: (a: number, b: number) => number
}

2. Implement the native interface

In C++:

class MathsModule: public MathsModuleSpec {
public:
  double getPi() override { return PI; }

  double multiply(double a, double b) override { return a * b; }
  double subtract(double a, double b) override { return a - b; }
  double add(double a, double b) override { return a + b; }
};

In Swift:

class MathsModule: MathsModuleSpec {
  var Pi: Double {
    return .pi
  }

  func multiply(a: Double, b: Double) -> Double { return a * b }
  func subtract(a: Double, b: Double) -> Double { return a - b }
  func add(a: Double, b: Double) -> Double { return a + b }
}

In Kotlin:

class MathsModule: MathsModuleSpec {
  val Pi: Double
    get() = PI

  fun multiply(a: Double, b: Double): Double { return a * b }
  fun subtract(a: Double, b: Double): Double { return a - b }
  fun add(a: Double, b: Double): Double { return a + b }
}

Platforms

Cross-platform

Cross-platform native modules can be built with C++. Any custom C++ types can be used and bridged to JS with minimal overhead.

JS <-> C++ type converters are statically generated ahead of time - no more dynamic lookups or runtime parser errors! 🥳

iOS

iOS native modules and view components can be written either in pure C++, or pure Swift. Thanks to Swift 5.9, Swift Nitro Modules bridge directly to C++ instead of going through Objective-C message sends. Woohoo, no more Objective-C, and zero overhead C++ -> Swift calls! 🥳

Android

Android native modules and view components can be written either in pure C++, or pure Kotlin/Java. Thanks to fbjni, even complex types can be effortlessly bridged to Kotlin/Java with minimal overhead! 🔥

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT