FlatList shows only a small part of a big list on the screen at a time. This saves memory and makes the app faster.
Why FlatList handles large datasets efficiently in React Native
import { FlatList, Text, View } from 'react-native'; function MyList() { const data = [{id: '1', title: 'Item 1'}, {id: '2', title: 'Item 2'}]; return ( <FlatList data={data} keyExtractor={item => item.id} renderItem={({item}) => <Text>{item.title}</Text>} /> ); }
FlatList only renders items visible on the screen plus a small buffer.
It uses a keyExtractor to identify items uniquely for efficient updates.
const data = [];
<FlatList
data={data}
keyExtractor={item => item.id}
renderItem={({item}) => <Text>{item.title}</Text>}
/>const data = [{id: '1', title: 'Only Item'}];
<FlatList
data={data}
keyExtractor={item => item.id}
renderItem={({item}) => <Text>{item.title}</Text>}
/>const data = Array.from({length: 1000}, (_, i) => ({id: String(i), title: `Item ${i}`})); <FlatList data={data} keyExtractor={item => item.id} renderItem={({item}) => <Text>{item.title}</Text>} />
This app shows 1000 items using FlatList. Only items visible on screen are rendered, so scrolling stays smooth and memory use stays low.
import React from 'react'; import { FlatList, Text, View, StyleSheet } from 'react-native'; export default function App() { const data = Array.from({length: 1000}, (_, index) => ({id: String(index), title: `Item ${index + 1}`})); return ( <View style={styles.container}> <FlatList data={data} keyExtractor={item => item.id} renderItem={({item}) => <Text style={styles.item}>{item.title}</Text>} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, paddingTop: 40 }, item: { padding: 10, fontSize: 18, borderBottomWidth: 1, borderColor: '#ccc' } });
FlatList renders only visible items plus a small buffer, reducing memory use.
Time complexity for rendering is better than rendering all items at once.
Common mistake: Using FlatList without a unique keyExtractor causes rendering issues.
Use FlatList instead of ScrollView for large lists to avoid slow performance.
FlatList improves performance by rendering only visible items.
It helps apps handle large lists smoothly and efficiently.
Always provide a unique keyExtractor for best results.
Practice
FlatList in React Native handle large datasets efficiently?Solution
Step 1: Understand rendering behavior of FlatList
FlatList renders only the items currently visible on the screen, not the entire list at once.Step 2: Effect on performance
This selective rendering reduces memory use and improves scrolling performance for large datasets.Final Answer:
Because it renders only the items visible on the screen -> Option DQuick Check:
Rendering visible items only = A [OK]
- Thinking FlatList loads all data at once
- Confusing FlatList with ScrollView
- Assuming FlatList disables scrolling
FlatList?Solution
Step 1: Understand keyExtractor purpose
keyExtractor must return a unique string for each item to help React identify items efficiently.Step 2: Evaluate options
keyExtractor={item => item.id.toString()} uses a unique id converted to string, which is correct. keyExtractor={item => Math.random()} uses random values causing instability. keyExtractor={item => 'key'} returns the same key for all items, which is wrong. keyExtractor={item => item.name + item.age} may not guarantee uniqueness.Final Answer:
keyExtractor={item => item.id.toString()} -> Option AQuick Check:
Unique stable keys = D [OK]
- Using random values for keys
- Using same key for all items
- Concatenating fields that may not be unique
FlatList without providing a keyExtractor when rendering a large list?Solution
Step 1: Default key behavior without keyExtractor
Without keyExtractor, FlatList uses the item index as the key by default.Step 2: Consequences of using index as key
Using index can cause rendering issues when items change order or are added/removed, leading to inefficient updates or UI glitches.Final Answer:
FlatList will use index as key but may cause rendering issues -> Option AQuick Check:
No keyExtractor = index keys = C [OK]
- Assuming FlatList crashes without keyExtractor
- Thinking FlatList auto-generates unique keys
- Believing all items render at once
FlatList is lagging when scrolling a large dataset. Which fix will improve performance?Solution
Step 1: Identify performance issue cause
Lag often happens because FlatList calculates item sizes dynamically, causing extra work during scrolling.Step 2: Use getItemLayout to optimize
Providing getItemLayout lets FlatList know item sizes upfront, improving scroll performance by skipping measurement.Final Answer:
Implement getItemLayout to optimize item measurement -> Option CQuick Check:
Use getItemLayout for smooth scrolling = A [OK]
- Removing keyExtractor which worsens performance
- Switching to ScrollView which loads all items
- Disabling virtualization causing memory issues
FlatList. Some messages have the same id due to server bugs. How should you ensure smooth rendering?Solution
Step 1: Understand duplicate key problem
Duplicate keys cause React to misidentify items, leading to rendering bugs and poor performance.Step 2: Create unique keys despite duplicates
Concatenating item.id with index ensures keys are unique even if ids repeat, maintaining FlatList efficiency.Final Answer:
Concatenate item.id with index in keyExtractor to ensure uniqueness -> Option BQuick Check:
Unique keys despite duplicates = B [OK]
- Ignoring duplicate keys causing UI bugs
- Removing duplicates which may lose data
- Switching to ScrollView losing performance benefits
Ready to prove it?
Pass the test and this pattern moves unknown
