React Nitro

Layout and safe areas

Position native views and draw their backgrounds, borders, and corners.

React Nitro uses the layout prop for measurements and the style prop for appearance. To draw a card, give it padding and a border width in layout, then choose its fill, border color, and corner radius in style.

import { Text, View } from 'react-nitro-renderer'

export function NoteCard() {
  return (
    <View
      layout={{ padding: 16, borderWidth: 1 }}
      style={{ background: 0xf5f3eaff, borderColor: 0xcac6baff, cornerRadius: 12 }}
    >
      <Text fontWeight="bold">Station garden</Text>
      <Text layout={{ marginTop: 4 }}>The first crocuses opened overnight.</Text>
    </View>
  )
}

Colors are numbers in 0xRRGGBBAA order. The last byte controls opacity. For example, 0xff000080 is red at roughly half opacity. fontSize and color belong directly on Text.

Size and align children

A column lays out children vertically. Set flexDirection: 'row' to place them side by side. justifyContent positions children along that direction and alignItems positions them across it. Give the outer screen flex: 1 to fill its parent. A child such as a thumbnail can have an explicit width and height in points.

Set flexWrap: 'wrap' on a container to move children onto a new line when they run out of space. The default is 'nowrap'. Use 'wrap-reverse' to stack lines in reverse order. Set gap in points to space both rows and columns. An explicit rowGap or columnGap overrides gap on that axis.

To overlay content, use position: 'absolute' with edge offsets in layout. To move a view visually while keeping the space layout reserved, set translateX or translateY in style.

Select a layout engine

Starting in alpha.16, apps use Lynx Starlight by default. Starlight and Yoga use the same renderer and layout props. When upgrading an existing app, check intrinsic sizing, rounding and absolute positioning inside static ancestors, which can produce different frames across engines.

To keep using Yoga, run bun add --exact react-nitro-layout-yoga@0.0.1-alpha.16 in the app directory. Set "layoutEngine": "react-nitro-layout-yoga" in app.json, run bunx react-nitro prepare, and rebuild the native app.

If you maintain a custom native view that exposes yogaNodePtr, expose layoutNodePtr from its native layout node. In renderer extensions, replace getViewYogaNode(view) with getViewLayoutNode(view) and pass the handle to the shared layout API. Rebuild custom native modules against the updated view spec.

Keep content inside device edges

Call useSafeAreaInsets() inside a screen. Add the edges your content needs to its padding. This header keeps its background all the way to the top edge while placing its title below the inset.

import { Text, View, useSafeAreaInsets } from 'react-nitro-renderer'

export function Header() {
  const insets = useSafeAreaInsets()
  return (
    <View
      layout={{
        paddingTop: insets.top + 8,
        paddingLeft: insets.left + 16,
        paddingRight: insets.right + 16,
        paddingBottom: 8,
      }}
      style={{ background: 0xe4efdcff }}
    >
      <Text fontSize={22}>Notebook</Text>
    </View>
  )
}

A navigation screen gets the insets for its own controller. Read them where you lay out that screen so its padding follows changes in the available space.

On this page