Performant Lists

Article · 2:00

Why ScrollView stops working, what FlatList does instead, and when FlashList is worth the dependency.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

A ScrollView mounts every child the moment it renders. Two hundred rows means two hundred components built before anything appears, and on a mid-range Android phone that is a visible freeze. The fix is virtualisation: build only the rows on screen, and recycle them as the reader scrolls.

FlatList is the built-in answer. Give it data and a renderItem, and it windows the list for you. Two things determine whether it is smooth. keyExtractor must return a stable unique id - using the array index means every insertion renumbers everything and the list re-renders wholesale. And renderItem must not create new work on every pass: define the row component outside, wrap it in React.memo, and pass it a stable onPress from useCallback.

SectionList is the same machinery with grouped data and sticky headers. getItemLayout is worth supplying when rows are a fixed height, because it lets the list skip measurement and makes scrollToIndex instant.

FlashList, from Shopify, is a drop-in replacement that recycles the underlying views rather than mounting and unmounting them, and on long lists of complex rows it is substantially faster. It is the right choice when a profiled FlatList is still dropping frames - not the default for a list of twenty items.

TS Example 1 54 lines
import { memo, useCallback } from 'react';
import { FlatList, View, Text, Pressable, StyleSheet } from 'react-native';

type Article = { id: string; title: string; author: string };

// Defined OUTSIDE, and memoised, so a parent re-render does not rebuild
// every visible row.
const Row = memo(function Row({
  article,
  onPress,
}: {
  article: Article;
  onPress: (id: string) => void;
}) {
  return (
    <Pressable onPress={() => onPress(article.id)} style={styles.row}>
      <Text style={styles.title}>{article.title}</Text>
      <Text style={styles.author}>{article.author}</Text>
    </Pressable>
  );
});

const ROW_HEIGHT = 72;

export function ArticleFeed({ articles }: { articles: Article[] }) {
  const onPress = useCallback((id: string) => console.log('open', id), []);

  return (
    <FlatList
      data={articles}
      // A stable id. Never the array index.
      keyExtractor={(item) => item.id}
      renderItem={({ item }) => <Row article={item} onPress={onPress} />}
      // Fixed-height rows: lets the list skip measuring entirely.
      getItemLayout={(_, index) => ({
        length: ROW_HEIGHT,
        offset: ROW_HEIGHT * index,
        index,
      })}
      ItemSeparatorComponent={() => <View style={styles.separator} />}
      ListEmptyComponent={<Text style={styles.empty}>Nothing here yet</Text>}
      onEndReachedThreshold={0.5}
      onEndReached={() => console.log('load the next page')}
    />
  );
}

const styles = StyleSheet.create({
  row: { height: ROW_HEIGHT, justifyContent: 'center', paddingHorizontal: 16 },
  title: { fontSize: 16, fontWeight: '600' },
  author: { fontSize: 13, color: '#6b7280' },
  separator: { height: 1, backgroundColor: '#e5e7eb' },
  empty: { padding: 24, textAlign: 'center', color: '#9ca3af' },
});

Open this example in the editor and change it. Nothing you type is saved.