Long lists: build only what's on screen
A phone shows a dozen rows at a time. Watch 10,000 contacts go into a ScrollView all at once, then into a FlatList that keeps only a window around the screen, and see what fast scrolling, jumping and keys do to it.
What you'll take away
- Pick a ScrollView for a handful of things and a FlatList for long lists, and say why
- Explain how
windowSizekeeps the mounted count flat, and why a fling shows blank rows - Use
getItemLayoutsoscrollToIndexreaches row 5,000 without measuring - Spot why index keys move a tick to the wrong row, and key rows by the data's id
The 1-minute version
Tap a point and the picture below plays that step.
A FlatList without getItemLayout can't jump to rows it hasn't rendered (scrollToIndex calls onScrollToIndexFailed)
10,000 contacts in a ScrollView with rows.map() open slowly and eat memory. Why?
The picture: a big phone standing in the night city with the contact list on its screen, drawn as its user sees it (a row not rendered yet is just empty screen). Up the street a warehouse holds the list's data, all 10,000 contacts; its roof carries every row as one strip, row 1 at the far left: blue where rows are mounted, white where the screen is. Batches of rows travel to the phone as lights, slower than in life so you can follow them. Rows mounted comes from React Native's defaults; what each row costs in memory depends on the row and the phone, so no megabytes are claimed.
Compare them all: Long lists
The same 10,000 rows: open the screen, scroll three screens, then jump to row 5,000. See how many rows each one mounts, and whether it can jump.
Use it when Short screens with a handful of things, like a form or settings
Skip it when Long or unknown-length data: every row is built up front
A model of the defaults in React Native's source: initialNumToRender 10, windowSize 21, 10 rows a batch every 50 ms. Rows mounted is the model's number, not measured megabytes. Press any tile to see it large above, with when it fits.
The props that matter
| Prop | Default | Does |
|---|---|---|
| keyExtractor | item.key, then item.id, then the index | gives each row its identity: use the data's id |
| initialNumToRender | 10 | rows before the first screen |
| windowSize | 21 | screens kept mounted: yours, 10 above, 10 below |
| maxToRenderPerBatch | 10 | rows added per batch while filling |
| getItemLayout | none | every row's offset, for fixed heights: no measuring, jumps anywhere |
In real code
// 10,000 contacts in a FlatList: only a window of rows around the screen is mounted. Stable keys, fixed-height rows
// described to the list with getItemLayout, and a row that doesn't re-render unless its own contact changes.
import { memo, useCallback, useRef } from "react";
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native";
type Contact = { id: string; name: string; phone: string };
const ROW = 56; // every row is exactly this tall
const Row = memo(function Row({ item, onOpen }: { item: Contact; onOpen: (id: string) => void }) {
return (
<Pressable accessibilityRole="button" onPress={() => onOpen(item.id)} style={styles.row}>
<Text>{item.name}</Text>
<Text style={styles.sub}>{item.phone}</Text>
</Pressable>
);
});
export function Contacts({ contacts, onOpen }: { contacts: Contact[]; onOpen: (id: string) => void }) {
const list = useRef<FlatList<Contact>>(null);
// One function for the life of the list, so memo'd rows aren't re-rendered for a new prop.
const renderItem = useCallback(({ item }: { item: Contact }) => <Row item={item} onOpen={onOpen} />, [onOpen]);
return (
<View style={styles.screen}>
<Pressable accessibilityRole="button" onPress={() => list.current?.scrollToIndex({ index: Math.floor(contacts.length / 2), animated: false })}>
<Text>Jump to the middle</Text>
</Pressable>
<FlatList
ref={list}
data={contacts}
renderItem={renderItem}
// A stable id, never the index: insert one at the top and every index-keyed row gets someone else's data.
keyExtractor={(c) => c.id}
// Fixed heights: the list knows every offset, so scrollToIndex can go anywhere without measuring first.
getItemLayout={(_, index) => ({ length: ROW, offset: ROW * index, index })}
// React Native's defaults, written out: 10 rows first, 21 screens kept mounted, 10 rows a batch.
initialNumToRender={10}
windowSize={21}
maxToRenderPerBatch={10}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1 },
row: { height: ROW, justifyContent: "center", paddingHorizontal: 16 },
sub: { color: "#6b7280" },
});
tsc against each library's own published types (react-native and the libraries this file imports); type-checked only, never run on a phoneIt printed: tsc 6.0.3 (strict): no errors against react-native 0.88.1 (type-checked only)A FlatList with stable keys, getItemLayout for fixed-height rows, a memo'd row and one renderItem, and the window's defaults written out. Type-checked with tsc against the types react-native 0.88 publishes; no phone ran it.
Common questions
ScrollView or FlatList?
A ScrollView suits a screen with a handful of things, since it builds everything at once. For long or unknown-length data use a FlatList, which builds only the rows around the screen.
How many rows does a FlatList keep mounted?
First initialNumToRender (10), then it fills a window of windowSize (21) screens: the one you see, 10 above, 10 below. The rest isn't mounted.
Why do blank rows show when I scroll fast?
A FlatList fills its window in batches, by default 10 rows every 50 ms. Scroll faster and you see rows not rendered yet. Light rows and memo help it keep up.
What does getItemLayout give me?
With rows of one height, you tell the list row i sits at length × i: no measuring, and scrollToIndex reaches any row at once. Without it, a jump to a row not yet rendered calls onScrollToIndexFailed.
What's wrong with the index as a key?
A key tells React which row is which piece of data. Insert or remove a row and every index shifts, so React leaves old state, like a tick, on other data. Use the data's own stable id.
When do I use a SectionList?
When the data comes in groups with headers, like contacts by letter or items by day. Headers stick to the top by default on iOS, and it virtualizes like a FlatList.
Next lesson
Animations on a phoneLevel: Intermediate
Rows are mounted only near the screen now; next, frames: who computes each one, and why JS-driven ones drop.