Skip to content
the poor coder

flatlist react native keyextractor

Poor Man
Published
Reading time
2 min read
On this page

Flatlist React Native KeyExtractor#

If you are working on a React Native project and using a FlatList component, you may have come across the "keyExtractor" prop. This prop is used to give each item in the list a unique identifier, which helps with rendering performance and detecting changes in the data.

What is keyExtractor?#

The "keyExtractor" prop is used to extract a key from each item in the list. This key is used to help React identify which items have changed, been added, or been removed from the list. By default, React uses the index of the item as its key, but this can lead to performance issues if the list is updated frequently.

How to use keyExtractor?#

To use keyExtractor, you need to define a function that takes an item from the list as its argument and returns a unique key for that item. The key can be any type, but it should be unique for each item in the list.

Text

<FlatList
  data={data}
  keyExtractor={(item) => item.id}
  renderItem={({ item }) => <Text>{item.title}</Text>}
/>

In this example, we define a keyExtractor function that takes an item from the data array and returns its "id" property as the key. This ensures that each item in the list has a unique key, which helps with performance and detecting changes in the data.

Other ways to use keyExtractor#

  • You can also use a combination of properties to create a unique key. For example, if your data contains both a "id" and a "name" property, you can concatenate them to create a unique key.
  • If your data doesn't have a unique key, you can use the "index" of the item as its key. However, this should be used as a last resort, as it can lead to performance issues if the list is updated frequently.
Text

const data = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Bob" },
];

<FlatList
  data={data}
  keyExtractor={(item) => item.id + item.name}
  renderItem={({ item }) => <Text>{item.name}</Text>}
/>

In this example, we concatenate the "id" and "name" properties of each item to create a unique key. This ensures that each item in the list has a unique key, even if the "id" property alone is not enough to create a unique key.

Conclusion#

The keyExtractor prop is an important tool for optimizing the performance of your FlatList component in React Native. By using a unique key for each item in the list, you can help React identify which items have changed, been added, or been removed from the list. Make sure to use a unique key for each item, and avoid using the index of the item as its key unless it is absolutely necessary.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC