FlatList helps show long lists smoothly. Optimizing it makes your app faster and uses less battery.
FlatList performance optimization in React Native
import { FlatList } from 'react-native'; <FlatList data={dataArray} renderItem={({ item }) => <ItemComponent item={item} />} keyExtractor={item => item.id.toString()} initialNumToRender={10} maxToRenderPerBatch={10} windowSize={21} removeClippedSubviews={true} getItemLayout={(data, index) => ( {length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index} )} />
initialNumToRender controls how many items render first to show quickly.
getItemLayout helps FlatList know item sizes to scroll faster.
<FlatList
data={items}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id.toString()}
initialNumToRender={5}
/><FlatList
data={items}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id.toString()}
removeClippedSubviews={true}
/><FlatList
data={items}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id.toString()}
getItemLayout={(data, index) => ({ length: 50, offset: 50 * index, index })}
/>This app shows 1000 items in a list. It uses FlatList props to render only a few items at a time, remove items outside the screen, and know each item's height. This makes scrolling smooth and fast.
import React from 'react'; import { FlatList, Text, View, StyleSheet } from 'react-native'; const DATA = Array.from({ length: 1000 }, (_, i) => ({ id: i.toString(), title: `Item ${i + 1}` })); const ITEM_HEIGHT = 50; export default function App() { const renderItem = ({ item }) => ( <View style={styles.item}> <Text>{item.title}</Text> </View> ); return ( <FlatList data={DATA} renderItem={renderItem} keyExtractor={item => item.id.toString()} initialNumToRender={10} maxToRenderPerBatch={10} windowSize={21} removeClippedSubviews={true} getItemLayout={(data, index) => ({ length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index })} /> ); } const styles = StyleSheet.create({ item: { height: ITEM_HEIGHT, justifyContent: 'center', paddingHorizontal: 20, borderBottomWidth: 1, borderColor: '#ccc' } });
Time complexity: FlatList renders only visible items, so rendering is O(visible items), not total items.
Space complexity: Memory use stays low by removing off-screen items with removeClippedSubviews.
Common mistake: Not using keyExtractor causes slow re-renders and warnings.
Use getItemLayout when item sizes are fixed to improve scroll performance.
FlatList shows long lists efficiently by rendering only visible items.
Use props like initialNumToRender, maxToRenderPerBatch, windowSize, and removeClippedSubviews to optimize performance.
Providing getItemLayout helps FlatList scroll faster when item sizes are fixed.
Practice
FlatList in React Native?Solution
Step 1: Understand FlatList's role
FlatList is designed to render lists efficiently by only rendering items visible on the screen.Step 2: Compare options
The other options describe unrelated features like animations, authentication, and navigation.Final Answer:
To efficiently render long lists by only rendering visible items -> Option AQuick Check:
FlatList optimizes rendering = A [OK]
- Thinking FlatList handles navigation
- Confusing FlatList with animation libraries
- Assuming FlatList manages user login
FlatList using getItemLayout?Solution
Step 1: Understand getItemLayout parameters
getItemLayout expects an object with length (item size), offset (distance from start), and index.Step 2: Calculate offset correctly
Offset should be item height multiplied by index to position items correctly.Final Answer:
getItemLayout={(data, index) => ({length: 50, offset: 50 * index, index})} -> Option CQuick Check:
Offset = item height * index = B [OK]
- Using index as offset directly
- Adding index to offset instead of multiplying
- Swapping length and offset values
<FlatList
data={data}
renderItem={({item}) => <Text>{item}</Text>}
initialNumToRender={5}
/>Solution
Step 1: Understand initialNumToRender prop
This prop controls how many items FlatList renders initially on screen.Step 2: Check the value given
initialNumToRender is set to 5, so FlatList renders 5 items first.Final Answer:
5 items -> Option AQuick Check:
initialNumToRender = 5 means 5 items rendered initially [OK]
- Assuming all items render at once
- Confusing initialNumToRender with maxToRenderPerBatch
- Thinking no items render until scroll
<FlatList
data={items}
renderItem={({item}) => <Text>{item.name}</Text>}
keyExtractor={(item) => item.id}
removeClippedSubviews={false}
/>Solution
Step 1: Understand removeClippedSubviews effect
Setting removeClippedSubviews to true helps FlatList remove off-screen items from memory, improving scroll performance.Step 2: Analyze current setting
Currently, removeClippedSubviews is false, which can cause slow scrolling due to extra memory use.Final Answer:
removeClippedSubviews should be true to improve performance -> Option DQuick Check:
removeClippedSubviews true improves scroll speed [OK]
- Ignoring removeClippedSubviews prop
- Using index as keyExtractor causing re-renders
- Thinking renderItem must return View only
Solution
Step 1: Use getItemLayout for fixed height items
Providing getItemLayout lets FlatList calculate item positions quickly, improving scroll speed.Step 2: Choose reasonable rendering batch sizes
initialNumToRender=10 and maxToRenderPerBatch=10 balance initial load and batch rendering without overloading memory.Step 3: Set windowSize to control offscreen rendering
windowSize=5 keeps a small buffer of items rendered outside viewport for smooth scrolling.Final Answer:
initialNumToRender=10, maxToRenderPerBatch=10, windowSize=5, getItemLayout provided -> Option BQuick Check:
Balanced props + getItemLayout = D [OK]
- Rendering all items at once causing lag
- Missing getItemLayout for fixed height items
- Setting windowSize too large or too small
Ready to prove it?
Pass the test and this pattern moves unknown
