Your first surface
A surface is a React component that the native app opens by name, full screen or as a block inside a native screen. In this tutorial for the React team you configure a project, write a surface called PointsHistory and register it. Run and develop then shows it running.
Before you start
Section titled “Before you start”You need a Nitro UI project written in TypeScript, on Nitro UI 0.3.0, the one version Nitro UI Brownfield supports. Nitro UI’s own project creator makes one with bunx create-nitro-ui@0.3.0 <Name>; an existing Nitro UI project works too, once its Nitro UI packages are pinned to 0.3.0. The project needs these packages:
react,nitro-ui-rendererandnitro-ui-cliindependencies, from Nitro UI. The renderer gives you components such asViewandText.nitro-ui-brownfieldindependencies. It gives youregisterSurfaceanduseSurface.nitro-ui-brownfield-cliindevDependencies. It puts thenuibcommand innode_modules/.bin.typescriptindevDependencies.nuib checkruns the project’s owntsc; without it, thetypescheck fails withtsconfig.json exists but no TypeScript compiler is installed (node_modules/.bin/tsc); add typescript to devDependencies.
You also need:
- Bun.
nuibruns from source with Bun; there is no build step. - For iOS: an Apple silicon Mac, Xcode and a booted simulator.
- For Android: the Android SDK and a running
arm64-v8aemulator.
The project also needs the patch step applied: Nitro UI Brownfield does not run on stock Nitro UI. Follow Install, step 6 once per project.
Physical devices are not part of this flow. You only need the tools of the platform you work on. Requirements lists every version.
Run nuib through Bun
Section titled “Run nuib through Bun”nuib is not on your PATH; a plain nuib in a shell gives command not found. Run it inside the React project, where node_modules/.bin/nuib exists:
bun run nuib checkEvery command on these pages is written in that form. Run outside a project that lists nitro-ui-brownfield-cli, Bun answers error: Script not found "nuib".
1. Configure the project
Section titled “1. Configure the project”All configuration lives in one key of the React project’s package.json: brownfield. Add it next to your dependencies.
"brownfield": { "name": "ShopReact", "version": "0.1.0", "ios": { "minimum": "16.0" }, "android": { "minSdk": 26, "group": "com.example", "artifact": "shop-react" }}| Field | Required | Rule |
|---|---|---|
name |
Always | An identifier: letters, digits and _, starting with a letter. It is the artifact’s name. The native team imports a module with this name and calls ShopReact.start. |
version |
Always | A semantic version such as 1.2.3 or 1.2.3-beta.1. |
ios.minimum |
To package iOS | The deployment target, "16.0" or newer. |
android.minSdk |
To package Android | An integer, 26 or higher. |
android.group, android.artifact |
To package Android | The Maven coordinate, such as com.example and shop-react. |
dev.port |
No | The dev server port, 1 to 65535. The default is 8081. |
hosts.ios, hosts.android |
No | The native apps that nuib dev ios and nuib dev android open. See Run and develop. |
registry |
No | The directory where nuib publish writes versions. See Package and publish. |
Add a platform section only for a platform you package. A missing key, an unknown field or an invalid value stops the command with a message that names the field and the file.
2. Write a component and register it
Section titled “2. Write a component and register it”A surface is an ordinary React component. Write it, export its props type, and register it under a name.
import type { ReactElement } from 'react'import { Text, View } from 'nitro-ui-renderer'
export interface PointsHistoryProps { userId: string filter?: 'lastMonth' | 'all' compact?: boolean}
const layout = { flex: 1, padding: 24 }
export function PointsHistory(props: PointsHistoryProps): ReactElement { return ( <View layout={layout}> <Text fontSize={26} fontWeight="bold">Points history</Text> <Text fontSize={15}>{`Member ${props.userId}`}</Text> </View> )}Register it in the project’s entry file:
import { registerSurface } from 'nitro-ui-brownfield'import { PointsHistory, type PointsHistoryProps } from './surfaces/PointsHistory'
registerSurface<PointsHistoryProps>('PointsHistory', PointsHistory)Importing nitro-ui-brownfield is enough to set it up. There is no separate init call.
Rules for registerSurface
Section titled “Rules for registerSurface”- Call it at the top level of a module, so the name exists as soon as the bundle has loaded.
- Write the name as a string literal. It starts with a letter, followed by up to 63 letters, digits,
_,.or-. Any other name throws aTypeError. - Use each name once in the project. Registering a name again replaces the component and logs a warning.
- Do not reuse the artifact’s name or a name of the native API, such as
SurfaceDescriptororBootPolicy.
3. Keep the props in JSON
Section titled “3. Keep the props in JSON”Props travel from native code to React as JSON. A prop type must therefore be something JSON can carry and that Swift and Kotlin can express as a type.
| Allowed in TypeScript | What the native app gets |
|---|---|
string, number, boolean |
A string, a double, a boolean |
A union of string literals: 'lastMonth' | 'all' |
An enum, named after its type alias or after the prop |
| An object (interface or type literal) of allowed properties | A value type: a Swift struct, a Kotlin data class |
An array of an allowed type: T[], Array<T>, readonly T[] |
An array or list |
An optional property: x?: T or x: T | undefined |
A parameter with a default value. The prop is left out when absent. |
Your editor catches part of this while you type: registerSurface refuses functions, Date and class instances. The full rules are applied by nuib check and nuib package. Each problem is reported with its place, and all of them are listed at once. The message has this format:
<file>:<line>:<column>: surface "PointsHistory", property "<path>": <reason>Two more limits apply to the values at run time:
- The props of one surface, written as compact JSON, take at most 16 KB. Put large data in shared state on the React side, not in props.
- Numbers must be finite.
NaNandInfinityare refused.
The component receives exactly the props object the native app passed. Nothing else is added to it. When the native app changes the props, the same React tree renders again with the new object, so component state is kept.
The order of the props matters to the native team too. Check the contract explains which changes break native code.
4. Read the surface identity with useSurface
Section titled “4. Read the surface identity with useSurface”The native app can open the same surface several times. Each open surface has its own id. useSurface returns the identity of the surface that renders the calling component.
import { useSurface } from 'nitro-ui-brownfield'
export function PointsHistory(props: PointsHistoryProps): ReactElement { const surface = useSurface() // surface.name is 'PointsHistory' // surface.id is the id of this open surface return ( <View layout={layout}> <Text fontSize={26} fontWeight="bold">Points history</Text> <Text fontSize={11}>{`${surface.name} ${surface.id}`}</Text> </View> )}| Field | Type | Meaning |
|---|---|---|
id |
string |
The id of this open surface. It stays the same for the life of the surface. |
name |
string |
The name you registered. |
You can call useSurface in any component below a registered surface. Outside a surface it throws an error.
Next: run it
Section titled “Next: run it”Your surface is written and registered. Now open it on a simulator or emulator, edit its props and see your changes with Fast Refresh.
bun run nuib dev ios --viewer- Run and develop: the viewer, the real native app in development, what you should see and troubleshooting.
- Check the contract: which changes to the props break native code.