Every String Lives in a Text

+15 Mana ✨

React Native is stricter about text than the web. Every string you render has to sit inside a Text component, and styles pass down only from a Text to the Text elements nested inside it, so the CSS habit of setting one font on html for the whole page doesn't carry over.

Key terms

  • Text node: a piece of raw text content in the rendered tree, similar to a text node in the DOM.
  • Nested text: a Text placed inside another Text, which is how React Native styles part of a string.
  • fontFamily: the text style that names the font a Text is drawn with.

Where a string is allowed

Every text node has to be wrapped in a Text, and a text node can't sit directly under a View. When one does, a development build logs this error with console.error and keeps rendering:

text
Text strings must be rendered within a <Text> component.

A production build skips the check and logs nothing, so you only see the mistake while developing. Nothing is thrown in either build, even though the Text page labels this case "will raise exception".

The check looks at where each string lands once your own components have rendered. A string counts as inside text when the nearest native element above it is a Text. That covers a string in a Text, a string in a nested Text, and a string passed to your own component when that component renders its children into a Text. A View between the string and the nearest Text breaks the chain, and the string counts as outside text again.

A label written in front of a value, the way you might on the web, trips the check:

tsx
<View>Total: <Text>{total}</Text></View>

Total: sits directly in the View. Moving the label into the Text fixes it:

tsx
<View><Text>Total: {total}</Text></View>

Styles flow down only inside Text

On the web, a font-family set on html reaches every element below it. React Native keeps style inheritance, but only inside text subtrees. A nested Text starts from its parent's styles and applies its own on top:

tsx
<Text style={{color: 'gray'}}>Your order is<Text style={{fontWeight: 'bold'}}> on its way</Text></Text>

The words "on its way" render gray and bold. React Native builds one string out of the nested elements, "Your order is on its way", and each piece of it carries its own styles. A plain string takes the styles of the Text that holds it.

Nesting is how you format part of a sentence. Android and iOS format text by annotating ranges of a string with styles such as bold or a color, which is tedious in practice, so React Native borrowed the web's approach and lets you nest Text.

Inheritance stops at the Text boundary. It lives inside the native Text component and doesn't leak to other components, so you can't set a default font for a whole subtree. The limit keeps components predictable: given the same props, a component looks the same wherever you drop it. fontFamily also takes a single font name, unlike CSS font-family, which takes a list of fallbacks.

Sharing a font with MyAppText

The recommended way to keep fonts and sizes consistent across an app is one component, MyAppText, that renders its children into a Text with the app's font and base size. Use it wherever you would write a plain Text, and build more specific components such as MyAppHeaderText on top of it. The code example below the lesson defines both, and the header nests a larger Text inside MyAppText:

tsx
<MyAppText>
  <Text style={{fontSize: 20}}>{children}</Text>
</MyAppText>

The inner Text keeps the app font and replaces the base size of 16 with 20. Composing components this way keeps the app-wide styles in one top-level component and still lets each specific use add to them or override them.

MyAppText sets fontFamily: 'Inter'. On Android, React Native looks for a file named Inter.ttf or Inter.otf in the app's assets/fonts folder, and falls back to the best matching system typeface when there is none. On iOS, when React Native finds no font family or single font named Inter, it draws the text in the system font without logging a warning.

Code Examples

tsx
import {Text, View, type TextProps} from 'react-native';

export function MyAppText({children}: TextProps) {
  return <Text style={{fontFamily: 'Inter', fontSize: 16}}>{children}</Text>;
}

export function MyAppHeaderText({children}: TextProps) {
  return (
    <MyAppText>
      <Text style={{fontSize: 20}}>{children}</Text>
    </MyAppText>
  );
}

export default function ReleaseNotes() {
  return (
    <View>
      <MyAppHeaderText>Version 2.4</MyAppHeaderText>
      <MyAppText>Bug fixes and faster sync.</MyAppText>
    </View>
  );
}
✓ Completed