Group a list into sections with Legend List
This tutorial builds a contacts list in an Expo app: 40 first names grouped under the letters A to F, with sticky section headers and a row of buttons that jump to a letter. It uses the SectionList of @legendapp/list 3.9.2, which Legend List 3.0 adds Web support and a SectionList component introduced.

The example is an Expo project. Its own files are App.tsx, src/data.ts, src/Contacts.tsx and __tests__/Contacts.test.tsx.
The sections
export type ContactSection = { title: string; data: string[] };
// Groups the names by their first letter: one section per letter, with the
// letter as its title. Sorting the names first puts the sections in order
// of their letters, and each section's names in order.
export function groupByLetter(names: readonly string[]): ContactSection[] {
const sections: ContactSection[] = [];
for (const name of [...names].sort((a, b) => a.localeCompare(b))) {
const letter = name[0].toUpperCase();
const last = sections[sections.length - 1];
if (last?.title === letter) last.data.push(name);
else sections.push({ title: letter, data: [name] });
}
return sections;
}src/data.ts starts with names, 40 first names in no particular order. groupByLetter sorts a copy with localeCompare. Then a name whose first letter is the last section's title joins that section's data, and any other name starts a new section, so the sections come out in letter order, each sorted.
The list
import { SectionList, type SectionListRef } from '@legendapp/list/section-list';
import { useRef } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { groupByLetter, names } from './data';
const sections = groupByLetter(names);
// The height of a section header, in px.
const HEADER_HEIGHT = 28;
export function Contacts() {
const list = useRef<SectionListRef>(null);
// Scrolls the list to the section. The section's header sticks to the top
// of the list and is HEADER_HEIGHT tall, so viewOffset keeps the section's
// first name clear of it.
function jumpTo(sectionIndex: number) {
list.current?.scrollToLocation({
sectionIndex,
itemIndex: 0,
viewOffset: HEADER_HEIGHT,
animated: true,
});
}
return (
<View style={styles.screen}>
<View style={styles.letters}>
{sections.map((section, sectionIndex) => (
<Pressable
key={section.title}
style={styles.letter}
onPress={() => jumpTo(sectionIndex)}
role="button"
aria-label={`Jump to ${section.title}`}
>
<Text style={styles.letterText}>{section.title}</Text>
</Pressable>
))}
</View>
<SectionList
ref={list}
role="region"
aria-label="Contacts"
style={styles.list}
sections={sections}
// Each name appears once, so it is its own key.
keyExtractor={(name) => name}
renderSectionHeader={({ section }) => (
<Text style={styles.header} role="heading">
{section.title}
</Text>
)}
renderItem={({ item }) => <Text style={styles.name}>{item}</Text>}
// Legend List's API reference: recycling reuses a row's component for
// another item, so state inside a row could carry over. These rows
// hold no state.
recycleItems
// Legend List's API reference: sticky headers are on by default only
// on iOS. This turns them on for every platform.
stickySectionHeadersEnabled
/>
</View>
);
}SectionList comes from @legendapp/list/section-list. Legend List's API reference says that it mirrors the props of React Native's SectionList. Contacts passes it:
sections, fromgroupByLetter;keyExtractor, the name itself, since each name appears once;renderSectionHeader, aTextwith the letter androle="heading";renderItem, aTextwith the name;stickySectionHeadersEnabled, so each header stays at the top of the list while its names scroll under it. The API reference says that sticky headers are on by default only on iOS, so the example turns them on everywhere.
recycleItems lets the list reuse a row's component for another name. The API reference warns that state inside a row could then carry over; these rows hold none. The tests find the list by its aria-label, "Contacts".
Each letter above the list is a Pressable with role="button" and an aria-label such as "Jump to D". It calls jumpTo, which calls scrollToLocation through a ref typed as SectionListRef: to the section's first item, itemIndex: 0, with viewOffset: HEADER_HEIGHT.
The header height
const styles = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#fff' },
letters: {
flexDirection: 'row',
justifyContent: 'space-around',
paddingVertical: 8,
borderBottomWidth: 1,
borderBottomColor: '#ddd',
},
letter: { paddingHorizontal: 12, paddingVertical: 6 },
letterText: { fontSize: 17, fontWeight: '600', color: '#2563eb' },
// The list takes the height left below the letters.
list: { flex: 1 },
// A line height equal to the height centers the header's one line of text.
header: {
height: HEADER_HEIGHT,
lineHeight: HEADER_HEIGHT,
paddingHorizontal: 16,
backgroundColor: '#f4f4f5',
color: '#555',
fontSize: 13,
fontWeight: '600',
},
name: {
paddingHorizontal: 16,
paddingVertical: 12,
borderBottomWidth: 1,
borderBottomColor: '#eee',
fontSize: 17,
},
});Every header is HEADER_HEIGHT, 28 px, tall, with the same line height to center its letter. The header sticks at the top of the list, so the jump passes the same value as viewOffset, to keep the section's first name clear of it. The screenshot script checks this: after "Jump to D", the D header's top is at the list's top edge, and the top of "Daisy", the first name under D, is at the header's bottom.
The app
import { StatusBar } from 'expo-status-bar';
import { StyleSheet } from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import { Contacts } from './src/Contacts';
export default function App() {
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Contacts />
<StatusBar style="auto" />
</SafeAreaView>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: '#fff' },
});App renders Contacts inside SafeAreaProvider and SafeAreaView, from react-native-safe-area-context.

This screenshot was taken right after the jump to D: the D header is at the top of the list, and "Daisy" sits right below it. The script then scrolls the list 40 px further with the mouse wheel and checks that the header stays at the top while a name under it moves up.
The tests
import { fireEvent, render, screen, userEvent } from '@testing-library/react-native';
import { Contacts } from '../src/Contacts';
import { groupByLetter, names } from '../src/data';
// Jest has no layout engine, so nothing measures the list. Like the layout
// engine, this gives the list a size: a 390 by 780 viewport.
function layOutList() {
return fireEvent.layout(screen.getByLabelText('Contacts'), { width: 390, height: 780 });
}
test('groupByLetter returns one section per letter, in order, each sorted', () => {
const sections = groupByLetter(names);
expect(sections.map((section) => section.title)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
for (const section of sections) {
expect(section.data.every((name) => name.startsWith(section.title))).toBe(true);
expect(section.data).toEqual([...section.data].sort());
}
expect(sections.flatMap((section) => section.data)).toHaveLength(names.length);
});
test('a jump button renders for each letter', async () => {
await render(<Contacts />);
const buttons = screen.getAllByRole('button');
expect(buttons).toHaveLength(6);
for (const letter of ['A', 'B', 'C', 'D', 'E', 'F']) {
expect(screen.getByRole('button', { name: `Jump to ${letter}` })).toBeOnTheScreen();
}
});
test('once the list has a size, it renders the first header and its names', async () => {
await render(<Contacts />);
expect(screen.queryByText('Aaron')).not.toBeOnTheScreen();
await layOutList();
expect(screen.getByRole('heading', { name: 'A' })).toBeOnTheScreen();
expect(screen.getByText('Aaron')).toBeOnTheScreen();
});
test('pressing "Jump to D" renders the names under D', async () => {
const user = userEvent.setup();
await render(<Contacts />);
await layOutList();
expect(screen.queryByText('Daisy')).not.toBeOnTheScreen();
await user.press(screen.getByRole('button', { name: 'Jump to D' }));
expect(screen.getByText('Daisy')).toBeOnTheScreen();
expect(screen.getByText('Dylan')).toBeOnTheScreen();
});The tests use the jest-expo preset and React Native Testing Library, with no other settings and no mocks. Jest has no layout engine, so nothing measures the list, and the third test checks that "Aaron" is not on the screen until the list has a size. layOutList gives it one with fireEvent.layout, which React Native Testing Library's docs describe as a way to simulate the layout engine measuring an element. Then the "A" header and its first name render.
The four tests check that:
groupByLetterreturns sections A to F, in order, each sorted, with names that start with its letter, and all the names;- one jump button renders per letter;
- once the list has a size, the "A" header and "Aaron" render;
- after "Jump to D" is pressed, "Daisy", which was not on the screen before, renders, and so does "Dylan".
The last test checks what the list renders, not where it scrolls.
The screenshot script
The jump and the sticky headers rest on the screenshot script, which opens the example's web export in Chromium through Playwright. It clicks "Jump to D", waits until the list stops scrolling, and fails unless the list has scrolled, the D header's top is within 2 px of the list's top edge, and the top of "Daisy" is within 2 px of the header's bottom. Then it scrolls the list 40 px further with the mouse wheel, and fails unless the D header stays at the list's top edge while "Dylan", a name under it, moves up.
Verified with Expo SDK 58 (React Native 0.88.1): type-checked, tested with Jest and React Native Testing Library, and exported for web.
Full example: reactnative/examples/sectioned-contacts