Quickstart
Show a React screen inside your native app. The React team gives you one artifact per platform. You add one dependency and call generated Swift or Kotlin functions. You never touch JavaScript, C++ or a bundler.
A React screen delivered this way is called a surface. In this guide the artifact is named ShopReact and it has a surface named PointsHistory. Replace both with the names of your own artifact.
The whole path, from zero
Section titled “The whole path, from zero”If nobody has made an artifact for you yet, this is the full path from an empty React project to a surface on screen in your app. The React steps are covered in Your first surface; this page covers the native steps.
-
React project. Create a Nitro UI project in TypeScript, add
nitro-ui-brownfield,nitro-ui-brownfield-cliandtypescript, and add thebrownfieldkey withname,versionandios.minimum. Apply the patches (Install, step 6). See Install and Your first surface. -
One surface. Write a component and register it with
registerSurface. See Your first surface. -
Package it. In the React project run:
Terminal window bun run nuib package --platform iosIt writes a Swift package to
<project>/.nuib/out/ios/ShopReact. The first run builds the engine and takes a few minutes; later runs reuse the cache in<project>/.nuib/cache. Add--ios-slices simulatorto build only the simulator slice while you try things. -
Add the package to your app as a local package (step 1 below), start the runtime (step 2) and open the surface (step 3).
-
Run on an Apple silicon simulator or a device (step 5).
-
React project. Create a Nitro UI project in TypeScript, add
nitro-ui-brownfield,nitro-ui-brownfield-cliandtypescript, and add thebrownfieldkey withname,versionandandroid.minSdk,android.group,android.artifact. Apply the patches (Install, step 6). See Install and Your first surface. -
One surface. Write a component and register it with
registerSurface. See Your first surface. -
Package it. In the React project run:
Terminal window bun run nuib package --platform androidIt writes a local Maven repository to
<project>/.nuib/out/android/maven, with the AAR under the coordinate<group>:<artifact>:<version>from yourbrownfieldkey. -
Add the repository and the dependency to your app (step 1 below), start the runtime in
Application.onCreate(step 2) and open the surface (step 3). -
Run on an
arm64-v8adevice or emulator (step 5).
For day-to-day work the React team does not repackage for every change: bun run nuib dev ios or bun run nuib dev android opens your app with the React project live in it. See Run and develop.
Before you start
Section titled “Before you start”If the React team gives you the artifact, ask them for these things. You need them in the steps below.
| You need | iOS | Android |
|---|---|---|
| Where the artifact is | The Swift package directory: <project>/.nuib/out/ios/ShopReact as nuib package writes it, or a published version in the registry, <registry>/ShopReact/<version>/swiftpm |
The Maven repository: <project>/.nuib/out/android/maven as nuib package writes it, or the registry’s <registry>/maven; and the coordinate <group>:<artifact>:<version> |
| The name to import | The module ShopReact |
The package <group>.<name in lower case>, for example com.example.shopreact |
| The surfaces | The name of each surface and its props. Each surface is a generated type, so your editor’s autocomplete shows them too. | The name of each surface and its props. Each surface is a generated type, so your editor’s autocomplete shows them too. |
Your project also needs:
- iOS: an Xcode project that can add Swift packages. The artifact sets a minimum iOS version, 16.0 or later. Your app must target at least that version.
- Android: a Gradle project that uses AndroidX fragments, and an
arm64-v8adevice or emulator. The artifact sets aminSdkof 26 or higher.
1. Add the dependency
Section titled “1. Add the dependency”Swift Package Manager. Add the artifact’s directory as a local package. In Xcode choose Add Package Dependencies, then Add Local, and select the directory (.nuib/out/ios/ShopReact, or swiftpm in a published version). Then link the product ShopReact to your app target.
If you declare dependencies in code:
dependencies: [ .package(path: "<React project>/.nuib/out/ios/ShopReact"),]That is all. Xcode embeds the framework itself. You add no C++ setting, no search path and no script phase.
The artifact has arm64 slices only: no x86_64 simulator slice. When you build for a generic simulator destination, for example in CI, add ARCHS=arm64 to xcodebuild; otherwise the build fails with Unable to resolve module dependency: 'ShopReact'.
Gradle. Add the Maven repository, then the dependency, and restrict the ABIs.
dependencyResolutionManagement { repositories { google() mavenCentral() maven { url = uri("<React project>/.nuib/out/android/maven") } }}android { defaultConfig { ndk { abiFilters += "arm64-v8a" } }}dependencies { implementation("<group>:<artifact>:<version>")}2. Start the runtime once, early
Section titled “2. Start the runtime once, early”The runtime is the part of the artifact that runs the React code. One runtime serves every surface in your app. Tell it to start when your app launches.
import ShopReact
// In your app delegate:// application(_:didFinishLaunchingWithOptions:)ShopReact.start(policy: .lazy)import com.example.shopreact.ShopReactimport nitroui.brownfield.BootPolicy
class App : Application() { override fun onCreate() { super.onCreate() ShopReact.start(this, BootPolicy.LAZY) }}The policy decides when the runtime boots:
.lazy/BootPolicy.LAZY(the default): nothing runs until the first surface opens. Your app launch costs nothing. The first surface pays for the boot..eager/BootPolicy.EAGER: the runtime boots insidestart. Launch takes longer and the first surface opens faster.
Call start once. A second call replaces the configuration and reboots the runtime, and every open surface mounts again.
3. Open your first surface full screen
Section titled “3. Open your first surface full screen”Each surface is a generated type. Its props are typed parameters: optional props have default values, a fixed set of strings is an enum, and numbers are Double. If you pass the wrong prop, your app does not compile.
A full-screen surface is a normal UIViewController on iOS and a normal Fragment on Android. You present it with your own navigation, so the back gesture and the back stack stay yours.
import ShopReact
let screen = PointsHistory.viewController( userId: "42", filter: .lastMonth, compact: false)navigationController? .pushViewController(screen, animated: true)import com.example.shopreact.PointsHistory
val screen = PointsHistory.fragment( userId = "42", filter = PointsHistory.Filter.LAST_MONTH, compact = false)supportFragmentManager.beginTransaction() .replace(R.id.content, screen) .addToBackStack(null) .commit()Here userId is required, and filter and compact are optional: this is the surface written in Your first surface. Your surface has its own props.
4. Embed a surface in a layout
Section titled “4. Embed a surface in a layout”A surface can also be a view inside one of your native screens, for example a card in a stack or a row in a list. Use view instead of viewController or fragment.
let block = PointsHistory.view( userId: "42", compact: true)stackView.addArrangedSubview(block)block.heightAnchor .constraint(equalToConstant: 240) .isActive = trueval block = PointsHistory.view( context, userId = "42", compact = true)block.id = R.id.points_blockcontainer.addView(block, layoutParams)- You set the size. The view does not report the size of its React content. Give it a width and a height with your constraints or layout params (
layoutParamsabove is yours), as you would for any view with no intrinsic size. - It opens by itself. The surface starts to load the first time the view enters a window. You do not call anything.
- On Android, give the view an id. The view saves its surface in its view state, and Android only saves the state of views that have an id.
5. Run it
Section titled “5. Run it”Build and run your app, then go to the screen that opens the surface. You should see the React screen fill the view controller or fragment, or fill the frame you gave the embedded view. With the lazy policy, the first surface takes a little longer than the next ones because it also boots the runtime.
To check what happens, watch the state of the surface. It moves from mounting to live when the first React render is on screen.
described mounting live
extension HomeViewController: SurfaceHostDelegate { func surfaceHost(_ host: any SurfaceHost, didChange state: SurfaceState) { print("\(host.descriptor.name) is \(state)") } func surfaceHost(_ host: any SurfaceHost, didReport timings: SurfaceTimings) {}}
// before you push the screenscreen.delegate = selfimport nitroui.brownfield.SurfaceHostimport nitroui.brownfield.SurfaceHostListenerimport nitroui.brownfield.SurfaceState
// before you commit the transactionscreen.listener = object : SurfaceHostListener { override fun onSurfaceStateChanged(host: SurfaceHost, state: SurfaceState) { Log.i("app", "${host.descriptor.name} is $state") }}A working surface logs mounting and then live. The library can also log one line per boot stage, in the form [brownfield] stage=<name> ms=<value>. On iOS the release artifact logs only when you turn logging on (see Turn on the logs), whatever the build configuration of your app. On Android a debuggable build of your app logs; the log tag is brownfield.
When nothing appears
Section titled “When nothing appears”A surface that cannot load does not crash your app. It moves to the failed state and, unless you gave it a fallback view, its container stays empty. So an empty screen usually means a failure you are not observing yet. Add the delegate or listener from step 5 first, then find your case here.
| What you see | Why | What to do |
|---|---|---|
The state stays mounting and never changes |
start has not run. A surface opened before start waits for it. |
Call ShopReact.start at app launch (step 2). On Android, the log says that the surface is waiting for BrownfieldRuntime.configure. |
Empty container, state is failed |
The surface failed and has no fallback view. | Log failure.cause and failure.message. The causes are explained in Surfaces in your app, under “Failure causes”. |
Embedded view is invisible, state is live |
The view has no size. | Give it a width and a height (step 4). |
| iOS: the delegate is never called | The host keeps its delegate weakly, and yours was released. | Keep the delegate alive, for example use the presenting view controller. |
Android: UnsatisfiedLinkError at launch or at the first surface |
The ABI filter is missing. | Add abiFilters += "arm64-v8a" (step 1). |
Android: IllegalStateException that says to use the runtime and surface hosts on the main thread |
You called the API from a background thread. | Call start and every host method on the main thread. |
| Android: Gradle cannot resolve the dependency | The Maven repository is not declared. | Add the artifact’s Maven repository to settings.gradle.kts (step 1). |
iOS: Unable to resolve module dependency: 'ShopReact' when building for a simulator |
A generic simulator destination also builds x86_64, and the artifact has no x86_64 slice. |
Add ARCHS=arm64 to xcodebuild, or build for one Apple silicon simulator. |
iOS: no [brownfield] lines at all |
The release artifact logs only when logging is turned on, even in a Debug build of your app. | Launch with -BrownfieldLogging YES (below). |
Turn on the library logs
Section titled “Turn on the library logs”- iOS: the release artifact, the one your app normally depends on, is silent until you enable logging, in Debug and Release builds of your app alike. The dev variant that
nuib devandnuib link --devinstall always logs. - Android: a debuggable build of your app logs. A release build is silent until you enable logging.
Launch argument:
-BrownfieldLogging YESShell:
adb shell setprop log.tag.brownfield DEBUGNext steps
Section titled “Next steps”- Surfaces in your app: update props, close a surface, and show a fallback and retry after a failure.
- Restore, debug and crashes: restore after Android process death, what Debug builds get, work with the React project live, and symbolicate a crash.
- Native API: facade, runtime and hosts and Native API: types: every type and function of the Swift and Kotlin API.
- The life of a surface: the states a surface goes through and what moves it from one to the next.
- Boot in stages: what the runtime does when it boots and how the boot policies differ.