Using Nitro in a library
Nitro can be used as a simple C++ library in your React Native library, which is very lightweight and simple.
1. Create a Nitro Module
First, you need to create a Nitro Module - either by just using the Nitro Modules template, or by just adding react-native-nitro-modules
to your existing React Native library.
2. Create Hybrid Objects
To actually use Nitro, you need to create Hybrid Objects - either by using Nitro's code-generator CLI “Nitrogen”, or by just manually extending the HybridObject
base class in C++.
3. Register Hybrid Objects
Each Hybrid Object you want to initialize from JS has to be registered in Nitro - either by autolinking them with Nitrogen (see Configuration (Autolinking)), or by manually registering the constructors in the HybridObjectRegistry
.
4. Use your Hybrid Objects in JS
Lastly, you can initialize and use the registered Hybrid Objects from JS. This is what this will ultimately look like:
interface Math extends HybridObject {
add(a: number, b: number): number
}
const math = NitroModules.createHybridObject<Math>("Math")
const value = math.add(5, 7) // --> 12