React Nitro

Text and input

Display labels, observe edits, and validate committed text.

Use Text for a label and TextInput for an editable field. A label accepts strings as children. Format counts and dates to strings before rendering them.

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

export function NoteTitle({ title }: { title: string }) {
  return (
    <Text fontSize={20} fontWeight="semibold" numberOfLines={2} ellipsizeMode="tail">
      {title}
    </Text>
  )
}

Text wraps to the available width. numberOfLines sets a line limit, and ellipsizeMode="tail" adds an ellipsis when the title exceeds it. lineHeight controls the distance between lines. To use a bundled typeface, import a .ttf or .otf file and pass it to font. Each file selects a specific face and weight. Rebuild the native app after adding or replacing the file.

Start and observe an edit

Set defaultValue to populate a field when it mounts. For an editor with multiple lines, pass multiline. The event passed to onTextChanged contains text, selectedTextRange, and markedTextRange. The first screen uses that event to update a preview.

onSelectionChanged also fires when the user moves the caret without editing the text. Ranges count UTF-16 code units, from an inclusive start to an exclusive end. A selection with equal endpoints is a caret. A marked range identifies text the keyboard is still composing.

Limit a short code

onBeforeTextChange receives both the previous and proposed text. Return the proposed text to accept it, the previous text to reject it, or a replacement string to transform it. This field accepts up to six ASCII letters or digits and displays them in uppercase.

import { TextInput } from 'react-nitro-renderer'

export function NoteCode() {
  return (
    <TextInput
      placeholder="Code"
      onBeforeTextChange={(edit) => {
        const valid = /^[a-z0-9]{0,6}$/i.test(edit.proposedText)
        if (valid) return edit.proposedText.toUpperCase()
        return edit.text
      }}
    />
  )
}

The decision runs synchronously. The keyboard can finish composing a character before the field validates the committed edit. For a programmatic reset, call setText('') on a TextInputRef. Programmatic changes bypass the edit callbacks, so update any separate React preview in the same action.

On this page