Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Lists and Platform-Specific Code  ›  Lesson

SectionList and Pull to Refresh

SectionList 12 minSectionList renders grouped data given as sections with a data array each, and pull to refresh needs a refreshing flag and an onRefresh handler that sets it back to false.

SectionList and Pull to Refresh

As Sprout grows, users want plants grouped by room: Kitchen, Bedroom, Balcony. They also expect the universal phone gesture: pull the list down, see a spinner, get fresh data. Two tools handle this: SectionList and pull-to-refresh.

The rule

Sections are titles with data arrays; refreshing needs a flag and a handler.

SectionList is a virtualized list with group headers. Its data is an array of sections, each with a title and its own data array:

const rooms = [
  { title: 'Kitchen', data: [basil, mint] },
  { title: 'Bedroom', data: [fern] },
  { title: 'Balcony', data: [cactus, tomato] },
];

<SectionList
  sections={rooms}
  keyExtractor={(plant) => plant.id}
  renderItem={({ item }) => <PlantCard plant={item} />}
  renderSectionHeader={({ section }) => (
    <Text style={styles.room}>{section.title}</Text>
  )}
  stickySectionHeadersEnabled
/>
  • The key must be named data inside each section. Other fields, like title, are yours to choose.
  • renderSectionHeader receives { section }, the whole section object.
  • Sticky headers keep the current room's title pinned at the top while you scroll. They are on by default on iOS.

Both list components only re-render when their props change. If you push a new plant into the existing array, the list may not notice. Build a new array instead, such as [...plants, newPlant], and regroup the sections from it.

Pull to refresh. FlatList and SectionList have it built in. Give them two props:

<FlatList
  data={plants}
  renderItem={renderPlant}
  refreshing={isRefreshing}
  onRefresh={reloadPlants}
/>

onRefresh runs when the user pulls down. Your handler sets isRefreshing to true, loads data, then sets it back to false. The spinner stays until the flag goes false, so forgetting that last step leaves it spinning forever.

For a plain ScrollView, use the RefreshControl component itself:

<ScrollView
  refreshControl={
    <RefreshControl refreshing={isRefreshing} onRefresh={reloadPlants} />
  }
>

The shortcut

When data has groups, reshape it before you render it: turn a flat array into [{ title, data }], then the SectionList props almost write themselves.

Grouping plants by roomStep 1 of 4
  1. Start flat

    plants is one array; each plant has a room field.

Drills

Answer fast — trust the rule.

quick drill

In a SectionList, what must each section's list of items be called?

quick drill

What does renderSectionHeader receive?

quick drill

After a pull to refresh, the spinner never stops. What is the likely bug?

quick drill

How do you add pull to refresh to a plain ScrollView?

Remember

  • Sections are { title, data }; refreshing needs a refreshing flag and onRefresh.
  • Reshape grouped data first, then render.
  • Trap: forgetting to set refreshing back to false.

Practice — level up

0/1 solved

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

ExplainGroup and refresh

Explain how you would turn Sprout's flat array of plants, each with a room field, into a SectionList grouped by room, and how you would add pull to refresh.