Master the art of creating smooth, performant lists that can handle thousands of items without lag. Your users will thank you!
Lists are everywhere in mobile apps, but poorly implemented lists can kill your app's performance. Let's learn how to build lightning-fast lists that can handle thousands of items!
The key to performant lists is proper FlatList configuration:
import React, { memo } from 'react';
import { FlatList } from 'react-native';
const ListItem = memo(({ item }) => (
<View style={styles.item}>
<Text>{item.title}</Text>
</View>
));
const PerformantList = ({ data }) => {
const renderItem = ({ item }) => <ListItem item={item} />;
const getItemLayout = (data, index) => ({
length: ITEM_HEIGHT,
offset: ITEM_HEIGHT * index,
index,
});
return (
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={(item) => item.id.toString()}
getItemLayout={getItemLayout}
initialNumToRender={10}
maxToRenderPerBatch={5}
windowSize={10}
removeClippedSubviews={true}
updateCellsBatchingPeriod={50}
/>
);
};
For more complex list requirements:
import { VirtualizedList } from 'react-native';
const CustomVirtualizedList = ({ data }) => {
const getItem = (data, index) => data[index];
const getItemCount = (data) => data.length;
return (
<VirtualizedList
data={data}
initialNumToRender={4}
renderItem={({ item }) => <ListItem item={item} />}
keyExtractor={(item) => item.id}
getItemCount={getItemCount}
getItem={getItem}
/>
);
};
Prevent memory leaks in your lists:
const OptimizedListItem = memo(({ item, onPress }) => {
const handlePress = useCallback(() => {
onPress(item.id);
}, [item.id, onPress]);
return (
<TouchableOpacity onPress={handlePress}>
<Text>{item.title}</Text>
</TouchableOpacity>
);
});
Your users will love the smooth scrolling! β‘
Help us improve our documentation