Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Ask "do I know how much content there is?" A short, fixed amount: ScrollView. An array that can grow: FlatList.

Two Sprout screensStep 1 of 4
  1. Settings

    Six fixed rows, written by hand. Small and known: ScrollView.

Drills

Answer fast — trust the rule.

quick drill

Why is FlatList better than ScrollView for 500 plants?

quick drill

Inside renderItem={({ item }) => ...}, what is item?

quick drill

What should keyExtractor return?

quick drill

Where does padding around a ScrollView's inner content belong?

quick drill

You want a big title above the plant list. What is the better approach?

Remember

  • ScrollView renders everything; FlatList renders what is near the screen.
  • Ask "do I know how much content there is?"
  • Trap: a FlatList inside a same-direction ScrollView.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich scrolling component?

Sprout has an 'About' screen with four paragraphs and a 'Watering history' screen that grows every day. Pick a component for each and explain the props you would use for the history.