Rendering scrollable lists of data effectively is a typical need in mobile applications. Whether you are creating a chat app, an e-commerce product listing, or a social media feed app, you require a high-performance means of displaying lists. That is where the React Native Flat List enters the picture.
In this entire tutorial, we will be dissecting how to utilize the React Native FlatList, what goes on behind the scenes, rendering various types of ListItems, and optimizing your list rendering for improved performance and user experience.
The FlatList is a fundamental element in React Native that efficiently displays big scrollable lists of items. It is the suggested method to display lists especially when the list size may grow dynamically or is large.
As opposed to the fundamental ScrollView, which displays all the items simultaneously, FlatList in React Native implements lazy loading. That is to say, it only loads items that are displayed on the screen (and a few off-screen items for more fluid scrolling), greatly enhancing performance.
Here’s why you need to use FlatList React Native over other list components:
Let’s look at a simple example of how to use a FlatList in React Native:
“`jsx
import React from ‘react’;
import { View, Text, FlatList, StyleSheet } from ‘react-native’;
const DATA = [
{ id: ‘1’, title: ‘First Item’ },
{ id: ‘2’, title: ‘Second Item’ },
{ id: ‘3’, title: ‘Third Item’ },
];
const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
const MyFlatList = () => {
return (
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.id}
/>
);
};
const styles = StyleSheet.create({
item: {
padding: 20,
backgroundColor: ‘#f9c2ff’,
marginVertical: 8,
},
title: {
fontSize: 18,
},
});
export default MyFlatList;
“`
Explanation
This example renders a vertical list of 3 items using React Native FlatList.
FlatList is a developer’s best friend for rendering extensive lists in mobile applications. Here’s why developers favor it:
A powerful feature of React Native Flat List is that you can create fully customized ListItems.
Here’s how to build a reusable React Native ListItem component:
“`jsx
const ListItem = ({ title, subtitle }) => (
<View style={styles.listItem}>
<Text style={styles.itemTitle}>{title}</Text>
<Text style={styles.itemSubtitle}>{subtitle}</Text>
</View>
);
const styles = StyleSheet.create({
listItem: {
padding: 16,
borderBottomWidth: 1,
borderColor: ‘#ccc’,
},
itemTitle: {
fontSize: 18,
fontWeight: ‘600’,
},
itemSubtitle: {
fontSize: 14,
color: ‘#666’,
},
});
“`
Now use it with the FlatList:
“`jsx
<FlatList
data={DATA}
renderItem={({ item }) => (
<ListItem title={item.title} subtitle=”Subtitle here” />
)}
keyExtractor={item => item.id}
/>
“`
This approach gives you complete control over how each list React Native item looks and behaves.
Reactnative Flatlist provides convenient props to add UI elements like headers, footers, and separators.
“`jsx
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.id}
ListHeaderComponent={<Text style={styles.header}>List Header</Text>}
ListFooterComponent={<Text style={styles.footer}>List Footer</Text>}
ItemSeparatorComponent={() => <View style={styles.separator} />}
/>
“`
“`jsx
const styles = StyleSheet.create({
header: {
fontSize: 24,
fontWeight: ‘bold’,
padding: 10,
backgroundColor: ‘#eee’,
},
footer: {
fontSize: 16,
padding: 10,
textAlign: ‘center’,
color: ‘#999’,
},
separator: {
height: 1,
backgroundColor: ‘#ccc’,
},
});
“`
To make the React Native FlatList scroll horizontally, just use the `horizontal` prop.
“`jsx
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.id}
horizontal={true}
/>
“`
You can build a photo slider, category tabs, or a horizontal newsfeed using this approach.
Pull to Refresh with FlatList React Native
Enable the pull-to-refresh feature with the `onRefresh` and `refreshing` props.
“`jsx
const [refreshing, setRefreshing] = useState(false);
const onRefresh = () => {
setRefreshing(true);
// fetch or update data
setTimeout(() => setRefreshing(false), 2000);
};
<FlatList
data={DATA}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.id}
refreshing={refreshing}
onRefresh={onRefresh}
/>
“`
1. Use `keyExtractor` Correctly
Unique keys make it easier for React to handle re-renders.
2. Use `initialNumToRender`
Manage how many items to render initially to prevent rendering delay.
3. Use `getItemLayout`
If your items are of fixed height, this enhances scroll performance.
4. Don’t Use Inline Functions
Move functions outside of render to prevent unnecessary re-renders.
5. Use `memo` and `PureComponent`
Memoize `ListItem` to prevent re-renders when props haven’t changed.
Both FlatList and ScrollView render lists of items, but they behave differently under the hood:
If you want grouped lists (like contacts sorted by alphabet), consider using `SectionList`, which is similar to Flatlist React Native but supports sections.
Error Message | Cause | Solution |
VirtualizedLists should never be nested | Using FlatList inside a ScrollView | Use nestedScrollEnabled={true} in ScrollView or avoid nesting altogether |
undefined is not a function (renderItem) |
renderItem is not defined or not a function |
Make sure renderItem is passed as a valid function |
Keys not unique | Duplicate or missing key values | Use a unique keyExtractor or ensure each item has a unique key property |
Cannot read property ‘length’ of undefined | data is undefined or not an array | Ensure data is a valid array before passing it to FlatList |
FlatList not rendering items |
renderItem or data is incorrect | Validate data is not empty and renderItem returns valid JSX |
ExtraData not causing re-render | FlatList doesn’t re-render on state change | Use extraData={yourState} to trigger re-renders on state update |
Performance issues with large data | Rendering too many items at once | Use initialNumToRender, maxToRenderPerBatch, and windowSize props |
It is obvious to implement React Native Flat List when it comes to building performance-scrollable list views. Its lazy rendering pattern is much ahead of rolling the individual arrays manually in the ScrollView. Whether you’re building a React Native ListItem-type feed or an engagement-product catalog, FlatList gives you performance, flexibility, and terse syntax.
Want help in building a scalable mobile app with FlatList and React Native? Contact our expert team at Logixbuilt Solutions for performance-driven React Native development.
1. What does FlatList in React Native serve the purpose of?
FlatList serves to render large or dynamic lists of items in a scrollable, optimized view.
2. How is FlatList different from ScrollView?
In contrast to ScrollView, FlatList only renders visible items, which decreases memory consumption and enhances performance.
3. Can I have multiple columns in FlatList?
Yes, utilize the `numColumns` prop to have a grid layout.
4. Is FlatList suitable for long lists?
Yes. FlatList is made to use long lists by only rendering visible items.
5. How do I include a separator between items in FlatList?
You can use the `ItemSeparatorComponent` prop to include a separator view.