Migration

Navigation and Fabric in a React Native to KMP Migration

Who controls what when React Native and Compose live in the same app, and how a Compose screen hands control back.

On this page

During a migration, the question is not "which framework is better at navigation". It is who owns the back stack at any moment. RN2KMP gives a simple answer: React Native does, and Compose reports to it.

Who controls what#

React Native owns

  • The router and the list of routes
  • The back stack and the system back button
  • Deep links and universal links
  • Transitions between screens
  • Which version of a screen is shown

Compose owns

  • Everything drawn inside the hosted view
  • State that belongs to that screen
  • Gestures and animations in its area
  • The decision to report that something happened

The boundary is the Fabric component. RN2KMP generates a React Native component for each @ReactNativeComponent, and a host on each platform (a FrameLayout containing a ComposeView on Android, a ComposeUIViewController on iOS) that puts your composable inside that view. React Native sets the props on the host, and the host recomposes the composable.

How the pieces connect#

text
React Native app
├── Router (Expo Router, React Navigation…)      ← owns routes, back, deep links
│   ├── /list      React Native screen
│   └── /detail    React Native route
│       └── <ItemDetail />      generated Fabric component
│           └── ComposeView / ComposeUIViewController
│               └── @Composable ItemDetail(...)   ← your Kotlin code
└── events flow back up: onBack, onOpenRelated → the route calls the router

A trip: React Native → Compose → React Native#

The detail route below is a normal route of your router. It renders the generated component, and turns the events of the component into router calls.

kotlin
// shared/src/commonMain/kotlin/ItemDetail.kt
@ReactNativeComponent
@Composable
fun ItemDetail(
    itemId: String,
    onBack: () -> Unit,        // becomes an event: onBack
    onOpenRelated: () -> Unit, // becomes an event: onOpenRelated
    modifier: Modifier = Modifier,
) {
    // Compose UI. It never calls a navigation API: it reports.
}
tsx
// app/detail/[id].tsx — a route of the React Native router
import { router, useLocalSearchParams } from 'expo-router';
import { ItemDetail } from 'react-native-kmp';

export default function Detail() {
  const { id } = useLocalSearchParams<{ id: string }>();
  return (
    <ItemDetail
      style={{ flex: 1 }}
      itemId={id}
      onBack={() => router.back()}
      onOpenRelated={() => router.push('/related')}
    />
  );
}
  1. The user taps a row on the React Native list. The list calls router.push('/detail/42').
  2. The router mounts the route. The route reads the parameters and renders ItemDetail with itemId="42".
  3. The user taps back inside the Compose screen. Compose calls onBack(). This crosses the bridge as an event.
  4. The route receives onBack and calls router.back(). The router pops the screen, as it would for any route.
This pattern is a recommendation The compiler guarantees that () -> Unit callbacks of a component become events, and that parameters become props. The routing code above is ordinary Expo Router code. In the Pokédex demo, the Compose list emits onPokemonClick(id), and Expo Router opens the detail route. Settings lets you switch either screen between React Native and Compose.

The trip above starts from the list. A deep link such as myapp://detail/42 opens the detail with no list behind it, for example from a cold start. The route does not know whether it has somewhere to go back to, so router.back() alone does nothing useful:

tsx
onBack={() => (router.canGoBack() ? router.back() : router.replace('/'))}

Decide what "back" means there (the list, the home screen) and test it from a cold start: it is the case that tells you whether the boundary is clean. The Compose component does not change: it still only reports onBack.

Because the router owns the routes, both keep working as they do for any other screen:

  • System back. The route is on the router's stack, so the system back button pops it. Do not register a second back handler in Compose for the same purpose: let Compose report the in-screen back (a button in your UI), and let the router handle the system one.
  • Deep links. A link such as myapp://detail/42 is resolved by the router to the /detail/42 route. The route reads 42 and passes it to the component as a prop. Compose never parses URLs.

Rules that keep the boundary clean#

  • Pass data in as props, report actions as events. The component does not reach out to the router.
  • Prefer one navigator. A Compose navigation graph inside a hosted view creates a second back stack, next to the router's. A self-contained flow (a short wizard, say) can have its own internal navigation if it defines clearly how it hands back to the router (events such as onFinished and onBack) and how system back and deep links reach it. If you are unsure, make each step a route.
  • Keep Kotlin defaults for Modifier and slots. React Native does not set them, so the compiler requires a default.
  • Use the same route for both versions of a screen. It makes the rollback a flag, and keeps deep links stable.

When Compose should own navigation#

At the end of the migration the router itself moves to Kotlin, and React Native goes away. Until then, routes in React Native are the stable part, and each migrated screen is a leaf you can replace.