Skip to the workspace
Pebellslearn by building
Core Components  ›  Lesson

View and Text

View 10 minView is the container that arranges content, while Text is the only component that shows words; nested Text inherits styles from its parent Text but not from a View.

react native / View and Text

Explore each scene at your own pace.

01 · The rule

The rule

View and Text

Every screen in Sprout, however fancy, is built mostly from two components. View arranges things and Text says things. They look like div and span, but each has habits of its own that trip up web developers.

The rule

View arranges and holds; Text shows words and nests other Text.

View is the basic building block of layout. It can hold any components, take a style, respond to layout and accessibility props, and it uses Flexbox by default. A View never shows text itself.

<View style={{ padding: 16, backgroundColor: '#eef7ee' }}>
  <Text style={{ fontSize: 20, fontWeight: 'bold' }}>Fern</Text>
  <Text>
    Next water: <Text style={{ fontWeight: 'bold' }}>tomorrow</Text>
  </Text>
</View>

Text has three habits worth learning:

  • Nesting. A Text inside a Text flows inline, like a span, and inherits the outer text's styles such as colour and font size. That is how "tomorrow" becomes bold mid-sentence above.
  • No inheritance from View. A color on a View does nothing for the Text inside it. Text styles only pass from Text to nested Text.
  • Truncation. numberOfLines={1} cuts long text with an ellipsis, handy for long plant notes.

Text also supports onPress, so a word can be tappable, and selectable to let users copy it.

Two more web habits do not carry over. A View never scrolls when its content is too tall; the extra content is simply cut off until you use a scrolling component. And text cannot be selected and copied unless you add selectable.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.