Docs

From an annotation to a React Native component

How rn2kmp turns a Kotlin composable into a React component, and why React Native keeps control of the app.

On this page

A migration is easier when you can see the whole mechanism. This article follows one annotation from your Kotlin code to a React component, using the architecture of the compiler.

One annotation#

You write a composable, and mark it:

kotlin
@ReactNativeComponent
@Composable
fun GreetingView(greeting: String, onTap: () -> Unit, modifier: Modifier = Modifier) {
    Button(onClick = onTap, modifier = modifier) { Text(greeting) }
}

Nothing else in this file knows about React Native. The annotation says "export this", and the rest of the work happens at build time.

A pipeline with one shared model#

text
Kotlin sources ──► frontend-ksp ──► registry.json (IR) ──► backends ──► generated files
 (annotated)        (KSP processor)                      TypeScript     React Native package
                                                          Android        + KMP androidMain / iosMain
                                                          iOS
  1. The frontend is a KSP processor. It reads the annotated declarations, checks that they can be bridged, and writes them to a registry.json.
  2. The intermediate representation holds the registry and a shared binding model. The binding model decides, once, the name of every method, event, type and parameter that crosses the bridge. TypeScript, Kotlin and Objective-C therefore always agree.
  3. The backends each turn the registry into files: the TypeScript API, the Android library, and the iOS pod.

Having one model and three backends is what keeps the generated code consistent. A rename is made in one place.

What the app gets#

For the GreetingView above, the generated npm package contains the TypeScript API, a Fabric component spec, an Android library and an iOS pod. In your Kotlin project, the compiler also writes a host for the component: a ComposeView on Android and a ComposeUIViewController on iOS. React Native sets the props on the host, and the host recomposes the composable.

The generated code uses the React Native New Architecture: TurboModules for functions and Fabric for components. On iOS it calls the Objective-C API that Kotlin/Native exports, and on Android it calls the Kotlin artifact.

Why React Native keeps control#

A Fabric component is a view, and a view lives inside a screen of the React Native router. The parameters of your composable become props. The callbacks that return Unit become events, which React Native handles. The Kotlin side never gets a handle on the router, because it has no way to ask for one.

This is a design choice, not a limitation to remove later: it is what lets half of an app live in each world. The router, the back stack and the deep links stay in one place, and each migrated screen is a leaf. The navigation page shows the pattern.

Try it#

Run the installation demo to see the generated package in use, or follow the installation guide to generate one for your own app.