A Comprehensive Guide to the FlatList Component in React Native

Khusni Ja'far
Tulisan Khusni
Published in
3 min readFeb 6, 2023

React Native is a powerful framework for building cross-platform mobile applications using JavaScript. One of the most important components in React Native is the FlatList component. FlatList provides a way to efficiently render large amounts of data in a scrolling list while maintaining the high performance of the application. In this article, we will dive into the details of FlatList and explore its features, usage, and best practices.

What is FlatList?

FlatList is a component that is used to display a scrolling list of items. It is similar to the ListView component in React Native, but with some important improvements. FlatList is more efficient than ListView because it only renders the items that are currently visible on the screen. This is achieved by using a virtualization technique that recycles the views that are no longer needed and reuses them for the new items. This technique results in a smoother and more efficient scrolling experience, especially when dealing with large amounts of data.

Features of FlatList:

  1. Performance: FlatList uses a virtualization technique to render only the items that are currently visible on the screen. This results in a much smoother and more efficient scrolling experience.
  • Flexibility: FlatList provides a lot of customization options, such as customizing the look and feel of the list items, adding headers and footers, and implementing pull-to-refresh.
  • Supports pagination: FlatList supports pagination, which is a way of loading more items as the user scrolls to the bottom of the list. This is useful when the list is very long and it would not be feasible to load all the items at once.
  • Reusable components: FlatList components can be reused, which helps to keep the code clean and organized.
  • Handles changes in data: FlatList can efficiently handle changes in the data that it is rendering. For example, when new items are added to the data array, FlatList can re-render only the items that have changed, instead of re-rendering the entire list.

How to use FlatList in React Native:

To use FlatList in React Native, you need to import it from the ‘react-native’ library. Then, you can define a FlatList component in your code, passing in the data that you want to render as well as some other props.

Here is a basic example of how to use FlatList in React Native:

import React from 'react';
import {FlatList, View, Text, StyleSheet} from 'react-native';

const data = [
{key: '1', name: 'Agus', age: 25},
{key: '2', name: 'Budi', age: 26},
{key: '3', name: 'Caca', age: 27},
{key: '4', name: 'Doni', age: 28},
{key: '5', name: 'Erik', age: 29},
{key: '6', name: 'Fandi', age: 30},
];

const Item = ({name, age}) => (
<View style={styles.item}>
<Text style={styles.name}>{name}</Text>
<Text style={styles.age}>{age}</Text>
</View>
);

const App = () => {
return (
<FlatList
data={data}
renderItem={({item}) => <Item name={item.name} age={item.age} />}
keyExtractor={item => item.key}
ListHeaderComponent={<Text style={styles.header}>People</Text>}
ListFooterComponent={<Text style={styles.footer}>End of list</Text>}
ItemSeparatorComponent={() => <View style={styles.separator} />}
onEndReached={() => {
console.log('End of list reached');
}}
onEndReachedThreshold={0.5}
/>
);
};

const styles = StyleSheet.create({
item: {
flex: 1,
flexDirection: 'row',
padding: 16,
alignItems: 'center',
},
name: {
fontWeight: 'bold',
fontSize: 18,
},
age: {
marginLeft: 16,
fontSize: 16,
},
header: {
fontWeight: 'bold',
fontSize: 24,
padding: 16,
backgroundColor: '#ddd',
},
footer: {
fontSize: 18,
padding: 16,
backgroundColor: '#ddd',
},
separator: {
height: 1,
width: '100%',
backgroundColor: '#ddd',
},
});

export default App;

In this example, we are using a custom component called Item to render each item in the list. The Item component takes the name and age props and returns a View component with two Text components inside. We are also using the keyExtractor prop to specify a unique key for each item in the data array. This is important because it helps FlatList keep track of which items have changed, which is essential for efficient rendering.

We are also using the ListHeaderComponent and ListFooterComponent props to add a header and footer to the list. The ItemSeparatorComponent prop is used to add a separator between each item in the list.

--

--