0
0
React Nativemobile~20 mins

FlatList basics (data, renderItem, keyExtractor) in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FlatList Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What will this FlatList display?
Consider this React Native FlatList component. What text will appear on the screen?
React Native
import React from 'react';
import { FlatList, Text, View } from 'react-native';

const data = [{id: '1', name: 'Apple'}, {id: '2', name: 'Banana'}];

export default function App() {
  return (
    <View>
      <FlatList
        data={data}
        keyExtractor={item => item.id}
        renderItem={({item}) => <Text>{item.name}</Text>}
      />
    </View>
  );
}
AThe screen shows an error because renderItem is missing.
BThe screen shows two lines: '1' and '2'.
CThe screen is blank because keyExtractor is missing.
DThe screen shows two lines: 'Apple' and 'Banana'.
Attempts:
2 left
💡 Hint
Look at what renderItem returns for each item.
📝 Syntax
intermediate
2:00remaining
Which keyExtractor is correct?
You want to use FlatList with this data: [{key: 'a'}, {key: 'b'}]. Which keyExtractor function will NOT cause a warning or error?
AkeyExtractor={item => item.key}
BkeyExtractor={item => item.id}
CkeyExtractor={item => item}
DkeyExtractor={item => item.toString()}
Attempts:
2 left
💡 Hint
The keyExtractor must return a string that exists in each item.
lifecycle
advanced
2:00remaining
What happens if keyExtractor returns non-unique keys?
If FlatList's keyExtractor returns the same key for multiple items, what is the likely result?
AFlatList will render all items correctly with no issues.
BFlatList will automatically fix keys to be unique.
CFlatList will show a warning and may render items incorrectly or skip updates.
DFlatList will crash the app immediately.
Attempts:
2 left
💡 Hint
Keys must be unique to help React identify items.
🔧 Debug
advanced
2:00remaining
Why does this FlatList show blank screen?
This FlatList code shows a blank screen. What is the problem? const data = [{id: '1', title: 'One'}, {id: '2', title: 'Two'}]; item.id} renderItem={() => {item.title}} />
Adata array is empty, so nothing renders.
BrenderItem does not receive 'item' parameter, so item.title is undefined.
CkeyExtractor returns wrong keys causing blank screen.
DFlatList requires a style prop to show content.
Attempts:
2 left
💡 Hint
Check how renderItem receives its argument.
🧠 Conceptual
expert
2:00remaining
Why use keyExtractor instead of index as key?
Why is it better to use a unique id in keyExtractor instead of the item index as key in FlatList?
AUsing unique ids prevents rendering bugs when list items change order or are added/removed.
BUsing index as key improves performance and prevents bugs.
CUsing index as key is required by FlatList and unique ids cause errors.
DUsing unique ids makes FlatList slower and less efficient.
Attempts:
2 left
💡 Hint
Think about what happens when list items reorder.