React Native Components — Mobile

React Native has no HTML tags. View is the counterpart of div (a layout box), Text holds all text, Image shows images (an image from the internet needs a…

On the web you build a page from <div>, <p>, <img> and <button>. React Native has no HTML tags at all. In their place come the core components, React Native's built-in building blocks: View, Text, Image, Pressable, TextInput, ScrollView and FlatList. While the app runs, each core component becomes a native iOS or Android view. You will compare two of them often. Picture Mrs. Rini's order book with 1,000 receipts. She doesn't pin every receipt on the wall at once. She only opens the page she's reading, plus a few pages around it, and turns pages when needed. That's how FlatList works. Pinning them all on the wall is ScrollView.

Core components and their web counterparts

React Native Web counterpart What it becomes on the phone
View a div that doesn't scroll UIView on iOS, ViewGroup on Android
Text p, span UITextView on iOS, TextView on Android
Image img UIImageView on iOS, ImageView on Android
TextInput input type="text" UITextField on iOS, EditText on Android
ScrollView a div with overflow: scroll UIScrollView on iOS, ScrollView on Android
Pressable button a View that responds to touch
FlatList ul + map, with virtualisation a ScrollView that only renders the rows near the screen

The first five rows follow the table in the React Native docs. View is the counterpart of div: a box for layout, and its layout uses flexbox.

Text must be inside Text

Web habit makes people write the first line below. Guess first: what shows up?

// Wrong: the text sits straight inside the View
<View>#13 Wulan</View>

// Right
<View>
  <Text>#13 Wulan</Text>
</View>

On the web, text can go anywhere. In React Native only Text knows how to lay out and draw letters, because it's the one that becomes a TextView or UITextView. A View can't do that. So in React Native 0.86, <View>#13 Wulan</View> shows nothing, and in development mode you get the error Text strings must be rendered within a <Text> component. The rule covers numbers too: {order.kg} has to be inside a Text.

To make part of a sentence bold, put a Text inside a Text:

<Text>Status: <Text style={{ fontWeight: 'bold' }}>Ironing</Text></Text>

Image, Pressable and TextInput

<Image source={require('@/assets/images/icon.png')} style={{ width: 48, height: 48 }} />
<Image source={{ uri: 'https://example.com/receipt-13.jpg' }} style={{ width: 120, height: 160 }} />

<Pressable onPress={() => console.log('open order 13')}>
  <Text>#13 Wulan</Text>
</Pressable>

<TextInput value={search} onChangeText={setSearch} placeholder="Search by name" />

The order list with FlatList

import { FlatList, Pressable, StyleSheet, Text, View } from 'react-native';
import { PRICE_PER_KG, ORDERS, formatPrice, type Order } from '@/data/orders';

function OrderRow({ order }: { order: Order }) {
  return (
    <Pressable style={styles.row} onPress={() => console.log('open order', order.id)}>
      <View>
        <Text style={styles.receipt}>#{order.id} {order.name}</Text>
        <Text>{order.kg} kg · {formatPrice(order.kg * PRICE_PER_KG)}</Text>
      </View>
      <Text style={styles.status}>{order.status}</Text>
    </Pressable>
  );
}

export default function Index() {
  return (
    <FlatList
      data={ORDERS}
      keyExtractor={(order) => order.id}
      renderItem={({ item }) => <OrderRow order={item} />}
    />
  );
}

const styles = StyleSheet.create({
  row: { flexDirection: 'row', justifyContent: 'space-between', padding: 16, backgroundColor: '#fff' },
  receipt: { fontSize: 16, fontWeight: '600' },
  status: { fontWeight: '600', color: '#1d4ed8' },
});

ORDERS lives in src/data/orders.ts: { id: '12', name: 'Mr. Darto', kg: 3, status: 'Washing' }, { id: '13', name: 'Wulan', kg: 2, status: 'Ironing' }, { id: '14', name: 'Yoga', kg: 5, status: 'Ready for pickup' }, PRICE_PER_KG = 7, and formatPrice(), which writes $14. The data is a sample, the three active orders on Mrs. Rini's board. In the API lesson, this list comes from the server.

Row keyExtractor renderItem shows
1 '12' #12 Mr. Darto, 3 kg · $21, Washing
2 '13' #13 Wulan, 2 kg · $14, Ironing
3 '14' #14 Yoga, 5 kg · $35, Ready for pickup

ScrollView or FlatList

For 3 orders, both behave the same. The difference shows with 1,000 orders. ScrollView renders all its children at once: 1,000 rows are created even though only 8 are visible. FlatList renders a first batch of 10 rows (initialNumToRender), then fills up to a windowSize of 21: the visible screen plus at most 10 screens above and 10 below. At the very top with 8 rows per screen, that's at most 8 + 10 × 8 = 88 rows. The rest are created only as you scroll, and the ones far behind are released from memory again.

So for long lists, use FlatList. ScrollView fits short content of a known size, such as the detail page of one order.

⚠️ Web habits that come along

TL;DR: React Native has no HTML tags. View is the counterpart of div (a layout box), Text holds all text, Image shows images (an image from the internet needs a width and a height), Pressable with onPress is for anything tappable, TextInput with onChangeText is for input. Text and numbers must be inside Text; otherwise they don't show and you get Text strings must be rendered within a <Text> component. For the order list use FlatList with data, keyExtractor (a unique string, such as order.id) and renderItem ({ item } per row). ScrollView renders all 1,000 rows, FlatList at most 88 at the top (8 rows per screen, windowSize 21).

⚡ No HTML tags

Meaning: In React Native, <div> is replaced by View: a box for layout. Text uses Text, images use Image, anything tappable uses Pressable with onPress, and input uses TextInput with onChangeText. While the app runs, each of these becomes a native iOS or Android view.

⚡ Text must be inside Text

Write:

<View>
  <Text>#13 Wulan</Text>
</View>
Guess first: if you write <View>#13 Wulan</View>, what shows up?

Nothing. Only Text can draw letters, and in development mode you get the error Text strings must be rendered within a <Text> component. Numbers too, so {order.kg} has to be inside a Text.

Meaning: Watch out for {count && ...}: when count is 0, React puts 0 straight into the View. Write {count > 0 && ...}.

⚡ FlatList for long lists

Write:

<FlatList
  data={ORDERS}
  keyExtractor={(order) => order.id}
  renderItem={({ item }) => <OrderRow order={item} />}
/>

Meaning: FlatList only renders the rows near the screen: out of 1,000 orders, at most 88 at the top. ScrollView renders all 1,000 rows at once, so it fits short content.

What you will learn