FlatList helps show a list of items on the screen in a smooth and fast way. It makes scrolling easy and efficient.
FlatList basics (data, renderItem, keyExtractor) in React Native
import { FlatList, Text, View } from 'react-native'; <FlatList data={arrayOfItems} renderItem={({ item }) => <Text>{item.name}</Text>} keyExtractor={item => item.id} />
data is the list of items you want to show.
renderItem tells how to show each item.
keyExtractor gives a unique key for each item to help React Native manage the list efficiently.
const fruits = [{ id: '1', name: 'Apple' }, { id: '2', name: 'Banana' }];
<FlatList
data={fruits}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id}
/>const emptyList = [];
<FlatList
data={emptyList}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id}
/>const singleItem = [{ id: '100', name: 'Only One' }];
<FlatList
data={singleItem}
renderItem={({ item }) => <Text>{item.name}</Text>}
keyExtractor={item => item.id}
/>This app shows a list of animal names using FlatList. Each animal has a unique id used as the key. The list is scrollable if needed.
import React from 'react'; import { FlatList, Text, View, SafeAreaView } from 'react-native'; export default function App() { const animals = [ { id: 'a1', name: 'Dog' }, { id: 'a2', name: 'Cat' }, { id: 'a3', name: 'Elephant' } ]; return ( <SafeAreaView style={{ flex: 1, marginTop: 50 }}> <FlatList data={animals} renderItem={({ item }) => <Text style={{ fontSize: 24, padding: 10 }}>{item.name}</Text>} keyExtractor={item => item.id} /> </SafeAreaView> ); }
FlatList only renders items visible on the screen for better performance.
Always provide a unique keyExtractor to avoid warnings and improve list updates.
Common mistake: forgetting keyExtractor or using non-unique keys causes rendering issues.
FlatList shows lists efficiently by rendering only visible items.
Use data to give the list items, renderItem to show each item, and keyExtractor to give unique keys.
This helps your app run smoothly and look good when showing lists.
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
