Native modules
Scaffold a local Nitro module and call its Swift and Kotlin implementations from React.
Use a local module when a feature needs a platform API. The CLI creates a package under Modules/, adds it to the app's dependencies, and runs Nitrogen to generate the native bindings. From the app directory, create a module named Greeter.
bun run react-nitro module GreeterFollow the call into native code
Open Modules/Greeter/src/Greeter.nitro.ts. It declares a constructible Greeter with a getMessage(): string method. The @iOS and @Android declarations name the implementations Nitrogen should bind.
On iOS, the method lives in ios/HybridGreeter.swift inside the module. On Android, it lives in android/src/main/java/com/margelo/nitro/app/greeter/HybridGreeter.kt. Each implementation returns Hello from Greeter. You can change that return value independently on each platform.
Display the result
Replace src/App.tsx with this screen, then rebuild and run the app. Constructing Greeter creates the native object for the current platform, and getMessage() calls its implementation.
import { Greeter } from 'app-greeter'
import { Text, View } from 'react-nitro-renderer'
const greeter = new Greeter()
export function App() {
const message = greeter.getMessage()
return (
<View layout={{ flex: 1, justifyContent: 'center', paddingHorizontal: 24 }}>
<Text>{message}</Text>
</View>
)
}Change the API
Edit the TypeScript spec when you add a method or change its signature. Run bun run react-nitro prepare to regenerate bindings, update the Swift and Kotlin implementations, and rebuild. The CLI's ios and android commands also prepare the app. When building directly in Xcode or Android Studio, run preparation yourself after a spec or configuration change.
Commit the spec, implementations, and module manifest. Nitrogen writes derived files under the module's nitrogen/generated/ directory. The CLI writes build settings and links under the app's .nitro/ directory. Edit the sources in Modules/Greeter/.