tezvyn:

FlatList: Performant Virtualized Lists in React Native

AI-drafted, machine-checkedSource: reactnative.devbeginner

FlatList is a "window" over your data, only rendering visible items to save memory and keep scrolling smooth. It's ideal for long lists like feeds or contacts. The main footgun: if items don't update, you forgot to pass changing state to the `extraData` prop.

WHY IT EXISTS: Rendering a list with thousands of items by mapping over an array would create thousands of component instances at once, consuming huge amounts of memory and likely crashing a mobile app. Mobile devices have limited resources, so a more efficient strategy is needed for long lists.

THE MENTAL MODEL: Think of FlatList as a virtualized list. It maintains a small "render window" of items that are currently on-screen or just off-screen. As you scroll, it unmounts components that scroll out of view and mounts new ones scrolling into view. This keeps memory usage low and constant, regardless of the list's total length.

HOW IT WORKS: FlatList requires two main props: data, an array of your list items, and renderItem, a function that tells the list how to render a single item from the data. To uniquely identify items for efficient updates, it uses a function you provide to the keyExtractor prop. Because it's a PureComponent for performance, it only re-renders when its props receive new references. If an item's rendering depends on state outside the data prop (like a selected item ID), you must pass this state via the extraData prop to tell FlatList that it needs to re-render its items.

WHEN TO USE IT: Use FlatList for any simple, long, or infinitely scrolling list of items that have a similar structure. Examples include a social media feed, a list of emails, a product gallery, or a contact list. Its built-in support for features like pull-to-refresh, separators, and multiple columns (numColumns) makes it very versatile.

WHEN NOT TO USE IT: If your list needs to be broken into logical sections with headers (like a contact list grouped by letter), use the <SectionList> component instead. For very short, static lists where performance is not a concern, a simple .map() inside a <ScrollView> might be simpler, though FlatList is generally the safer default.

ONE CANONICAL EXAMPLE: To render a simple list of users, you provide an array of user objects to the data prop and a function to renderItem. The renderItem function receives an object containing the item (e.g., {id: '1', name: 'Alice'}) and returns a component displaying the user's name. You must also provide a keyExtractor function, like item => item.id, to give each rendered element a stable identity for React's reconciliation process.

Read the original → reactnative.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.