When to use keyExtractor prop in React Native's FlatList

Published on Mar 27, 2022

5 min read

REACT-NATIVE

cover_image

In React Native, the FlatList component works well to render a long list of data. It renders only the items are shown on the screen in a scrolling list and not all the data items at once.

To render a scrollable list of items using FlatList, you need to pass the required data prop to the component. The data prop accepts an array of items. Each item in the array represents a single item in the list. Another required prop is renderItem, which takes an item from the data and renders it on the list. This prop accepts a function that returns the JSX to be rendered.

To display an item in the scrollable list, the FlatList component requires that each item has a unique key such as an id. This key is what allows the FlatList component (since it uses VirtualizedList under the hood) to track the order of items in the list. The key from the data array is extracted using the keyExtractor prop on the FlatList component.

In this post, let's talk about where you might need to use keyExtractor and what scenarios it is not required.

Display a list of items using FlatList

🔗

Consider the following structure of data. There are ten items in the array, and each item has two properties, id and title. The id is the unique key for each item.

1const DATA_WITH_ID = [
2 {
3 id: 1,
4 title: 'quidem molestiae enim'
5 },
6 {
7 id: 2,
8 title: 'sunt qui excepturi placeat culpa'
9 },
10 {
11 id: 3,
12 title: 'omnis laborum odio'
13 },
14 {
15 id: 4,
16 title: 'non esse culpa molestiae omnis sed optio'
17 },
18 {
19 id: 5,
20 title: 'eaque aut omnis a'
21 },
22 {
23 id: 6,
24 title: 'natus impedit quibusdam illo est'
25 },
26 {
27 id: 7,
28 title: 'quibusdam autem aliquid et et quia'
29 },
30 {
31 id: 8,
32 title: 'qui fuga est a eum'
33 },
34 {
35 id: 9,
36 title: 'saepe unde necessitatibus rem'
37 },
38 {
39 id: 10,
40 title: 'distinctio laborum qui'
41 }
42];

Using the FlatList component, you want to render the title of each item as shown below:

1export default function App() {
2 const renderList = ({ item }) => {
3 return (
4 <View style={styles.listItem}>
5 <Text style={styles.listItemText}>{item.title}</Text>
6 </View>
7 );
8 };
9
10 return (
11 <View style={styles.container}>
12 <FlatList data={DATA_WITH_ID} renderItem={renderList} />
13 </View>
14 );
15}

The result of the above component will display a list of items without any errors or warnings. In addition, the FlatList component doesn't require a unique key to identify each item since the original data structure already contains a key called id.

Here is the output on a device's screen from the above snippet:

ss2

Using the keyExtractor prop

🔗

By default, the keyExtractor prop checks for properties like key and id (in that order). If any of the two is present in the original data structure, it will be considered a the unique key by the FlatList component. In this case(as in the previous example), you do not have to explicitly use the keyExtractor prop.

If none of them are provided, the FlatList component will throw a warning "VirtualizedList: missing keys for items ...":

ss1

Now, let's consider a scenario where array of data contains a unique key with each list item but the name of the unique key is neither key nor id. It contains a unique key property with the name of userId.

1const DATA_WITH_USER_ID = [
2 {
3 userId: 1,
4 title: 'quidem molestiae enim'
5 },
6 {
7 userId: 2,
8 title: 'sunt qui excepturi placeat culpa'
9 },
10 {
11 userId: 3,
12 title: 'omnis laborum odio'
13 },
14 {
15 userId: 4,
16 title: 'non esse culpa molestiae omnis sed optio'
17 },
18 {
19 userId: 5,
20 title: 'eaque aut omnis a'
21 },
22 {
23 userId: 6,
24 title: 'natus impedit quibusdam illo est'
25 },
26 {
27 userId: 7,
28 title: 'quibusdam autem aliquid et et quia'
29 },
30 {
31 userId: 8,
32 title: 'qui fuga est a eum'
33 },
34 {
35 userId: 9,
36 title: 'saepe unde necessitatibus rem'
37 },
38 {
39 userId: 10,
40 title: 'distinctio laborum qui'
41 }
42];

When rendering the list, you will see the warning in this case because the FlatList component doesn't recognize the userId as the key or id name in the original data structure.

For custom key names such as userId in the example above, the keyExtractor prop is used. It extracts the unique key name and its value and tells the FlatList component to track the items based on that value.

For the above array of data, modify the FlatList component and use the keyExtractor prop to extract the key:

1<FlatList
2 data={DATA_WITH_ID}
3 renderItem={renderList}
4 keyExtractor={item => item.userId}
5/>

The warning will also disappear after this step.

Conclusion

🔗

When using a FlatList component, if the data array has a unique id or a key property, you do not need to use the keyExtractor prop explicitly. However, for custom id names, use the keyExtractor prop to explicitly tell the component which unique key to extract.

If you like to learn more about React Native, check out the React Native category and Expo category pages on my blog. You can also subscribe my newsletter or follow on Twitter to get updates on whenever I publish a new article or tutorial.


More Posts

Browse all posts

Join 1300+ devs & subscribe to my newsletter!

NEWSLETTER

🔗 Subscribe on Revue

Aman Mittal author

Software Developer and Technical Writer since 2017. Loves learning and writing about Node.js, React, React Native & Expo. Previously, worked as Developer Advocate, independent consultant, and technical writer with companies like Draftbit, Vercel and Crowdbotics.


Copyright ©  2019-2022 Aman Mittal · All Rights Reserved.