Skip to the workspace
Pebellslearn by building
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.

react native / SectionList and Pull to Refresh

Explore each scene at your own pace.

01 · The rule

The rule

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} />
  }
>
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.