What if you could build long, smooth lists with just a few lines of code?
Why FlatList basics (data, renderItem, keyExtractor) in React Native? - Purpose & Use Cases
Imagine you want to show a list of your favorite movies on your phone app. You try to add each movie one by one manually in your code, writing separate components for each movie.
This feels like writing a long list by hand every time you want to add or remove a movie.
Manually adding each item is slow and boring. If you want to change the list, you must rewrite many parts. It's easy to make mistakes like forgetting to update the order or missing an item.
Also, the app can become slow if you try to show many items this way because it tries to load everything at once.
FlatList helps by taking your list of data and automatically creating the list items for you. You just tell it what data to use, how to show each item, and how to identify each item uniquely.
This way, you write less code, avoid mistakes, and the app runs smoothly even with many items.
return ( <View> <Text>Movie 1: Inception</Text> <Text>Movie 2: Interstellar</Text> <Text>Movie 3: Dunkirk</Text> </View> );
return (
<FlatList
data={movies}
renderItem={({item}) => <Text>{item.title}</Text>}
keyExtractor={item => item.id.toString()}
/>
);FlatList makes it easy to build fast, scrollable lists that update automatically when your data changes.
Think about a contacts app that shows hundreds of friends. FlatList helps show all contacts smoothly and lets you add or remove friends without rewriting the whole list.
Manually coding lists is slow and error-prone.
FlatList automates list creation using your data and a render function.
It improves app performance and makes your code cleaner.
Practice
keyExtractor prop in a React Native FlatList?Solution
Step 1: Understand the role of
ThekeyExtractorkeyExtractoris used to give each item a unique key, which helps React track which items changed, added, or removed.Step 2: Differentiate from other props
renderItemdefines how to show items,datais the list source, and tap handling is done separately.Final Answer:
To provide a unique key for each list item to help React identify items. -> Option DQuick Check:
keyExtractor = unique keys [OK]
- Confusing keyExtractor with renderItem
- Using index as key without keyExtractor
- Thinking keyExtractor handles item display
FlatList with data and renderItem props?Solution
Step 1: Check prop names
The correct props aredatafor the list andrenderItemfor rendering each item.Step 2: Check renderItem function parameter
renderItemreceives an object withitemproperty, so it should be({item}) => ....Final Answer:
<FlatList data={items} renderItem={({item}) => <Text>{item}</Text>} /> -> Option AQuick Check:
Correct props and destructuring = C [OK]
- Using wrong prop names like 'items' or 'render'
- Not destructuring item in renderItem
- Passing item directly without braces
const data = [{id: '1', name: 'Apple'}, {id: '2', name: 'Banana'}];
<FlatList
data={data}
keyExtractor={item => item.id}
renderItem={({item}) => <Text>{item.name}</Text>}
/>
Solution
Step 1: Analyze data and keyExtractor
Data has two objects with ids and names. keyExtractor returns item.id, which is correct.Step 2: Analyze renderItem output
renderItem showsitem.name, so it will display 'Apple' and 'Banana'.Final Answer:
A list showing 'Apple' and 'Banana' as text items. -> Option BQuick Check:
renderItem shows item.name = Apple, Banana [OK]
- Thinking keyExtractor missing causes blank screen
- Confusing item.id with item.name in renderItem
- Assuming renderItem is missing or wrong
FlatList usage?
const data = [{id: 'a'}, {id: 'b'}];
<FlatList
data={data}
renderItem={({item}) => <Text>{item.id}</Text>}
/>
Solution
Step 1: Check for keyExtractor
The code does not provide akeyExtractor, so React Native will warn because keys are needed for list items.Step 2: Validate other props
renderItem correctly destructuresitem, data can be objects, and style is optional.Final Answer:
Missing keyExtractor prop, which may cause warnings or errors. -> Option CQuick Check:
keyExtractor missing = warning [OK]
- Ignoring missing keyExtractor warnings
- Thinking renderItem destructuring is wrong
- Believing style prop is mandatory
keyExtractor to avoid key conflicts in FlatList?
const users = [
{id: '1', name: 'Alice'},
{id: '1', name: 'Bob'},
{id: '2', name: 'Charlie'}
];
<FlatList
data={users}
keyExtractor={item => item.id}
renderItem={({item}) => <Text>{item.name}</Text>}
/>
Solution
Step 1: Identify duplicate keys problem
Using onlyitem.idcauses duplicate keys for '1', which React warns against.Step 2: Fix keyExtractor to ensure uniqueness
Combiningitem.idwithindexcreates unique keys like '11', '12', '20'.Final Answer:
Change keyExtractor to combine id and index: (item, index) => item.id + index -> Option AQuick Check:
Unique keys prevent warnings = Change keyExtractor to combine id and index:(item, index) => item.id + index[OK]
- Ignoring duplicate keys causing warnings
- Using only index loses stable keys
- Removing keyExtractor causes errors
Ready to prove it?
Pass the test and this pattern moves unknown
