Docs

Annotations

See what RN2KMP can export and annotate a first Kotlin function and Compose component.

On this page

First exports#

In commonMain:

kotlin
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import dev.rn2kmp.annotations.ReactNativeComponent
import dev.rn2kmp.annotations.ReactNativeFunction

@ReactNativeFunction
fun sayHello(to: String): String = "Hello, $to!"

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

The annotations#

Annotation Exports
@ReactNativeFunction A public function, or an eligible constructor.
@ReactNativeProperty A public property. Also renames the fields of an exported value type.
@ReactNativeClass A class or interface by reference, or an object as a singleton.
@ReactNativeType A class by value, or an enum by its entries.
@ReactNativeComponent A public @Composable function returning Unit, as a React component.

Every annotation takes an optional name to rename the export. Public names must match [A-Za-z_$][A-Za-z0-9_$]*. Top-level exports share a namespace, so rename Kotlin overloads individually to export them together.

Components#

A component is a public composable that returns Unit.

  • Ordinary parameters become props.
  • Non-suspending callbacks without a receiver that return Unit become events.
  • A Modifier parameter and composable slots must have Kotlin defaults: React Native does not set them.

For type mappings, object handles and compiler limits, see Supported types.