You can render a list by mapping an array to elements, and the same map works inside a ScrollView. A ScrollView renders every child at once, though. Mapping 2,000 contacts creates the components and native views for all 2,000 rows up front, most of which may never be shown, and that slows rendering and increases memory use. FlatList takes the array as a prop and creates rows as they are needed.
Key terms
renderItem: the FlatList prop that turns one item ofdatainto the element for its row.- Render window: the finite set of rows a FlatList keeps rendered around the visible area, with blank space of the right size standing in for every row outside it.
keyExtractor: a function with the signature(item, index) => stringthat returns a row's key, which FlatList uses for caching and as the React key that tracks reordering.extraData: a FlatList prop for any value outsidedatathat the rows depend on, so the list knows to re-render when it changes.
Rows render as they are about to appear
FlatList renders items lazily, when they are about to appear, and removes the ones that scroll far off screen to save memory and processing time. It is built for long lists of similarly structured data whose length can change over time, such as contacts shaped like {id: 'c1', name: 'Amara Okafor'}. The array goes in data, and renderItem draws each row:
tsx<FlatList data={contacts} renderItem={({item}) => <Text>{item.name}</Text>} />
data takes an array, or an array-like list, of items. The object passed to renderItem has three fields. item is the element of data being rendered, index is its position in the data array, and separators holds functions for the row's separators. FlatList also accepts a ListItemComponent in place of renderItem. With neither one set, every row renders nothing.
Blank space and lost row state
Content outside the visible area is rendered asynchronously, so if you scroll faster than the list can fill in, you may briefly see blank space where rows will appear.
A row's internal state is lost when it scrolls out of the render window. The rows of the first render batch, 10 by default, are the exception, because FlatList never unmounts them. Keep what a row has to remember, such as whether it is expanded, in the item data or in a store outside the row.
windowSize sets how much is rendered around the visible area, counted in viewport lengths, not items. The default, 21, covers the visible screen plus up to 10 screens above and 10 below it. initialNumToRender sets the size of the first render batch.
Where row keys come from
When you map an array yourself, you put a key on each element. FlatList finds the keys for you. It calls your keyExtractor when you pass one, and otherwise uses a default extractor that checks, in order:
item.key, if the item is an object with akeyitem.id, if it has anid- the index, as a string
Items that aren't objects, such as plain strings, always get their index. Each contact has an id, so its row is keyed by contact.id. You don't have to copy the id into a key field.
When the identifier has another name, pass a keyExtractor that returns it:
tsx<FlatList data={messages} keyExtractor={message => message.uuid} renderItem={({item}) => <Text>{item.body}</Text>} />
extraData for state the rows read
The ContactPicker example below the lesson keeps selectedId in state, outside data, and colors the row whose id matches it. FlatList is a PureComponent, so it doesn't re-render while its props stay shallow-equal.
Here renderItem is written inline, so it is a new function on every render and the props change anyway. The example still passes extraData={selectedId}, because the list needs it as soon as renderItem has a stable reference, for example when it is memoized or defined outside the component. Whatever you pass must not be === to the old value after an update, so treat it immutably. If you track an array of selected ids, replace it with a new array rather than pushing into the old one.
Content above and below the rows
FlatList is a wrapper around VirtualizedList, a more flexible base list component that SectionList wraps too. It accepts all of the ScrollView props, including keyboardShouldPersistTaps and horizontal, which lays the items out side by side. What it shouldn't do is sit inside a plain ScrollView that scrolls the same way. That nesting logs this error:
textVirtualizedLists should never be nested inside plain ScrollViews with the same orientation because it can break windowing and other functionality - use another VirtualizedList-backed container instead.
A VirtualizedList-backed container is a list built on VirtualizedList, such as a FlatList or a SectionList. To scroll a title or a search field along with the contacts, let the list scroll everything and pass that content as ListHeaderComponent or ListFooterComponent, which render at the top and bottom of all the items.
Code Examples
import {useState} from 'react';
import {FlatList, Pressable, StyleSheet, Text, View} from 'react-native';
type Contact = {id: string; name: string};
const CONTACTS: Contact[] = [
{id: 'c1', name: 'Amara Okafor'},
{id: 'c2', name: 'Lucas Martin'},
{id: 'c3', name: 'Priya Shah'},
];
export default function ContactPicker() {
const [selectedId, setSelectedId] = useState<string | null>(null);
return (
<View style={styles.screen}>
<FlatList
data={CONTACTS}
renderItem={({item}) => (
<Pressable
onPress={() => setSelectedId(item.id)}
style={[styles.row, {backgroundColor: item.id === selectedId ? '#dbeafe' : '#ffffff'}]}>
<Text>{item.name}</Text>
</Pressable>
)}
extraData={selectedId}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: {flex: 1},
row: {padding: 16},
});