Skip to the workspace
Pebellslearn by building
Lists and Platform-Specific Code  ›  Lesson

ScrollView and FlatList

FlatList 12 minScrollView renders all its children at once and suits short fixed content, while FlatList virtualizes a data array so only rows near the screen are rendered.

react native / ScrollView and FlatList

Explore each scene at your own pace.

01 · The rule

The rule

ScrollView and FlatList

On the web, a long page scrolls by itself. On a phone, nothing scrolls unless you ask. Sprout's settings screen is short and fixed; its plant list might hold five plants or five hundred. React Native has two main answers, and picking the wrong one makes a big list slow.

The rule

ScrollView renders everything; FlatList renders only what is near the screen.

ScrollView is a scrollable View. Every child is created at once, even the ones far off screen:

<ScrollView contentContainerStyle={{ padding: 16 }}>
  <Text>Settings</Text>
  <Text>Units</Text>
  <Text>About Sprout</Text>
</ScrollView>

That is fine for a short, known amount of content. Styles that shape the inner content, such as padding, go in contentContainerStyle. Add horizontal to scroll sideways.

FlatList is for data lists. It virtualizes: it creates rows only for items on or near the screen, and recycles memory as you scroll.

<FlatList
  data={plants}
  keyExtractor={(plant) => plant.id}
  renderItem={({ item }) => <PlantCard plant={item} />}
  ItemSeparatorComponent={() => <View style={{ height: 8 }} />}
  ListEmptyComponent={<Text>No plants yet. Add one!</Text>}
/>
  • data is the array.
  • renderItem receives an object; the current element is its item field (plus index).
  • keyExtractor returns a unique string for each item so React can track rows.
  • ListEmptyComponent, ListHeaderComponent and ListFooterComponent fill the obvious gaps.
  • onEndReached fires near the bottom, for loading more.

FlatList can also scroll sideways with horizontal, or show a grid with numColumns={2}, which suits a wall of plant photos.

Do not put a FlatList inside a ScrollView that scrolls the same way. The outer one forces every row to render, which defeats virtualization, and React Native warns about it. Use the list's header and footer props instead.

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