Migrate a React Native screen
Port a screen with View, Text and Pressable, validate the target and bake a deterministic first frame.
Keep the React component, its hooks and its state. Change imports of supported primitives from react-native to @bake-native/react. Hook imports still come from react. Start with one screen so each build diagnostic points to the code you are moving.
Port a screen
bake create counter --targets web,ios,android
cd counter
pnpm installReplace the scaffold’s src/App.tsx with this screen:
import { useState } from 'react';
import { Pressable, Text, View } from '@bake-native/react';
export function App() {
const [count, setCount] = useState(0);
return (
<View>
<Text>Count: {count}</Text>
<Pressable accessibilityRole="button" onPress={() => setCount(n => n + 1)}>
<Text>Add one</Text>
</Pressable>
</View>
);
}Supported React Native APIs
API from @bake-native/react | Supported behavior |
|---|---|
View | Flex-column container. Accepts children, style, className, nativeID, testID, accessibilityLabel, accessibilityHint, accessibilityRole and accessibilityState. |
Text | Text and nested text with the common props above, plus text styling such as fontSize, fontWeight and lineHeight. |
Image | The common props without children, plus source, resizeMode and alt. |
Pressable | The common props, onPress and disabled. A press event can have coordinates when the host supplies them. |
StyleSheet | create, compose and flatten. Use supported style objects or arrays. Numeric dimensions and axis shorthands such as paddingHorizontal are supported. |
List | One column of fixed-height rows in the page scroll. Provide data, renderItem({ item, index }) and itemHeight. Optional props are keyExtractor, initialCount, overscan, className and recycleItems (default false). See Long lists. |
Write the screen with these built-in views. Use supported HTML elements when you reuse existing web markup or need an element that no built-in view covers. Use built-in views first explains what each choice gains and gives up.
On native targets, the baked frame creates the built-in View container before JavaScript runs. React reuses that container when the first commit matches. Use a class for styles shared between web and native View containers.
Import a local image and pass it as source. For a remote image, pass a single { uri } object. Image supports cover, contain, stretch, and center for resizeMode.
import { Image } from '@bake-native/react';
import cover from './cover.png';
<Image source={cover} resizeMode="contain" alt="Album cover" style={{ width: 96, height: 96 }} />Give remote images a width and height to reserve their space before the download. Native image decoding runs asynchronously. Include a visible first-screen element while the image loads.
To port a fixed-height React Native FlatList, keep data, renderItem and keyExtractor. Replace getItemLayout with one positive itemHeight and use initialCount for initialNumToRender. When rows depend on state outside the list, have the row read that state through React props or context. You can keep the JSX component name with a local import alias:
import { List as FlatList } from '@bake-native/react';
<FlatList data={items} itemHeight={64} renderItem={renderRow} />Use onLayout on a View or Pressable to read its border box. The callback receives event.nativeEvent.layout with x and y relative to the parent and the box’s width and height. It runs on mount and when those dimensions or offsets change. Scrolling and CSS transforms leave the reported layout unchanged. On the web, view timeline offsets preserve fractional coordinates. When those offsets are unavailable, the browser’s layout offsets give integer CSS-pixel positions. To measure a text label, place the listener on a wrapping View. Render a visible initial value while waiting for the first measurement so the compiler can bake the screen.
Move function-valued styles and children into React state and pass supported values to the primitive. Features such as onLongPress and arbitrary native refs need a supported alternative. Use TextInput from Universal UI for a controlled input. Third-party components that use additional React Native host APIs need to be ported before they can run.
If your screen embeds a custom platform control, declare it as a native view. For a view named Swatch, import Swatch and the type SwatchRef from ../native/views/generated/Swatch.js in src/App.tsx.
To pass the first-frame bake on a chart-only screen, draw a visible SVG shape or show a label such as <Text>Loading chart</Text> until the platform view attaches after adoption. SVG <text> depends on the host’s glyph support. If the first screen contains only SVG lettering, convert it to filled paths or add a visible text label outside the SVG.
pnpm exec bake check
pnpm exec bake build --bakeThe check validates target compatibility. The native build also runs the first render in Hermes and writes the first commit to app.baked. It checks the snapshot and the first adopted screen in both light and dark mode. If a color scheme hides your only content, the build reports the scheme and the app source location.
Render deterministic initial content, such as a saved-items screen with a pending count. Because the host decodes bundled and remote images asynchronously, show visible text, a painted foreground element, or an inline <svg> on the first frame of an image-based screen. Read device data in a React effect after the first commit or in an event handler. If the first render throws or never commits, the build fails with the cause and guidance for the affected target.
Choose the UI imports
| Import | What it provides |
|---|---|
@bake-native/react | React Native-style primitives, StyleSheet and ordinary app helpers such as native |
@bake-native/react/ui | Universal components such as Host, Column, Text and Button |
@bake-native/react/ui/swift-ui | SwiftUI components and modifiers for Apple targets, including VStack and padding |
For example, this Universal component uses the same save callback on every target:
import { Button, Column, Host, Text } from '@bake-native/react/ui';
function Settings({ save }: { save: () => void }) {
return (
<Host>
<Column spacing={8}>
<Text>Settings</Text>
<Button label="Save" onPress={save} />
</Column>
</Host>
);
}For a platform-specific control, see SwiftUI components. Declare app fonts in bake.config.ts. Native services use the existing module registry. For example, bake add haptics copies the spec and implementations into the app, and the build generates registration for the modules your bundle calls. To embed this screen as an independently packaged feature, see Micro-native packages.
From a tap to a native update
- The native host delivers the press event to the registered handler in Hermes. Calling the state setter schedules work in React.
- React reconciles the next element tree against its previous tree. During commit, the BakeNative React renderer records changes to host props, text and children.
- The renderer submits the committed mutations as a binary batch over JSI. The shared Rust core computes the affected style, layout and paint work. The host applies the resulting operations to its views.
The first visible frame takes a separate path. The host lays out and commits the baked frame before starting Hermes on the main thread. When React’s first live commit matches the baked tree, the host adopts the existing views. Subsequent commits use the update path above. See First-screen snapshots and Adoption.