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
Textinside aTextflows inline, like aspan, 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. Acoloron aViewdoes nothing for theTextinside it. Text styles only pass fromTextto nestedText. - 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.