What is the purpose of the keyExtractor prop in a FlatList?
The keyExtractor prop provides a unique key for each item in the list. This helps React Native efficiently update and render only changed items, improving performance.
Click to reveal answer
intermediate
Why should you avoid anonymous functions in FlatList's renderItem?
Using anonymous functions inside renderItem causes new functions to be created on every render, which can slow down the list. Instead, define the function outside or use useCallback to memoize it.
Click to reveal answer
beginner
What does the initialNumToRender prop control in FlatList?
It controls how many items are rendered initially when the list loads. Setting it too high can slow startup, too low can cause blank space while scrolling.
Click to reveal answer
intermediate
How does getItemLayout help optimize FlatList?
getItemLayout lets FlatList know the height and position of each item in advance. This avoids measuring on the fly and improves scroll performance.
Click to reveal answer
intermediate
What is the benefit of using React.memo with FlatList item components?
React.memo prevents re-rendering of list items if their props haven't changed. This reduces unnecessary work and makes scrolling smoother.
Click to reveal answer
Which prop helps FlatList identify each item uniquely for better performance?
Explain how you would optimize a FlatList to improve scrolling performance in a React Native app.
Think about reducing unnecessary renders and helping FlatList know item sizes.
You got /5 concepts.
Describe the role of the keyExtractor and why it is important for FlatList optimization.
Keys help React know which items changed or stayed the same.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using keyExtractor in a React Native FlatList?
easy
A. To define the layout height of each item
B. To provide a unique key for each list item to improve rendering performance
C. To set the initial number of items to render
D. To enable pull-to-refresh functionality
Solution
Step 1: Understand the role of keys in lists
Keys help React Native identify which items changed, added, or removed, improving rendering speed.
Step 2: Identify what keyExtractor does
keyExtractor returns a unique key for each item, which is essential for efficient list updates.
Final Answer:
To provide a unique key for each list item to improve rendering performance -> Option B
Quick Check:
Unique keys = keyExtractor [OK]
Hint: Keys uniquely identify items for smooth list updates [OK]
Common Mistakes:
Confusing keyExtractor with initialNumToRender
Using index as key causing performance issues
Thinking keyExtractor sets item layout
2. Which of the following is the correct way to set initialNumToRender in a FlatList?
easy
A. <FlatList initialNumToRender='10' />
B. <FlatList initialNumToRender={"10"} />
C. <FlatList initialNumToRender={10} />
D. <FlatList initialNumToRender>10</FlatList>
Solution
Step 1: Understand prop types in React Native
Numeric props must be passed as numbers, not strings.
Step 2: Check each option's syntax
<FlatList initialNumToRender={10} /> passes a number correctly with curly braces. Options A and C pass strings incorrectly. <FlatList initialNumToRender>10</FlatList> is invalid JSX syntax.
Final Answer:
<FlatList initialNumToRender={10} /> -> Option C
Quick Check:
Numeric props use curly braces [OK]
Hint: Use curly braces for numbers in JSX props [OK]
Common Mistakes:
Passing numbers as strings in props
Using invalid JSX syntax for props
Confusing prop names with event handlers
3. Given this FlatList snippet, what will be the effect of adding getItemLayout?
A. Improves scroll performance by letting FlatList skip measurement of item sizes
B. Causes a runtime error because getItemLayout is missing required props
C. Makes FlatList render all items at once
D. Disables scrolling in the list
Solution
Step 1: Understand getItemLayout purpose
It tells FlatList the size and position of each item upfront, avoiding costly layout calculations during scroll.
Step 2: Analyze the provided getItemLayout function
The function returns fixed length and offset based on index, which is valid and improves performance.
Final Answer:
Improves scroll performance by letting FlatList skip measurement of item sizes -> Option A
Quick Check:
getItemLayout = faster scroll [OK]
Hint: Fixed item sizes speed up scrolling [OK]
Common Mistakes:
Assuming getItemLayout causes errors without checking signature
Thinking it renders all items immediately
Believing it disables scrolling
4. You notice your FlatList is lagging when scrolling. Which of these fixes addresses a common mistake causing this?
medium
A. Implementing getItemLayout with fixed item sizes
B. Setting initialNumToRender to a very high number
C. Removing keyExtractor and using array index as key
D. Using inline anonymous functions in renderItem
Solution
Step 1: Identify common causes of lag in FlatList
Using unstable keys or rendering too many items initially can cause lag.
Step 2: Evaluate each option's effect
Removing keyExtractor and using array index as key worsens performance by using unstable keys. Setting initialNumToRender to a very high number renders too many items at once. Using inline anonymous functions in renderItem causes re-renders due to new function references. Implementing getItemLayout with fixed item sizes improves performance by optimizing layout calculations.
Final Answer:
Implementing getItemLayout with fixed item sizes -> Option A
Quick Check:
Fixed layouts = less lag [OK]
Hint: Avoid inline functions and use getItemLayout [OK]
Common Mistakes:
Using array index as key causing re-renders
Setting initialNumToRender too high
Ignoring getItemLayout optimization
5. You have a large list with items of varying heights. Which combination of FlatList optimizations is best to improve performance without layout errors?
hard
A. Use getItemLayout with fixed height and no keyExtractor
B. Use keyExtractor with index as key and getItemLayout assuming fixed height
C. Set initialNumToRender very high and skip keyExtractor
D. Use keyExtractor with unique IDs and initialNumToRender set to a small number, but avoid getItemLayout
Solution
Step 1: Understand challenges with varying item heights
Fixed height assumptions in getItemLayout cause layout errors if items vary in size.
Step 2: Evaluate options for best practice
Use keyExtractor with unique IDs and initialNumToRender set to a small number, but avoid getItemLayout. This uses unique keys and limits initial render count, avoiding getItemLayout to prevent errors. Use keyExtractor with index as key and getItemLayout assuming fixed height uses index keys and fixed height, causing bugs. Set initialNumToRender very high and skip keyExtractor renders too many items and lacks keys, hurting performance. Use getItemLayout with fixed height and no keyExtractor lacks keys and assumes fixed height, causing errors.
Final Answer:
Use keyExtractor with unique IDs and initialNumToRender set to a small number, but avoid getItemLayout -> Option D
Quick Check:
Unique keys + small initial render + no fixed layout [OK]
Hint: Avoid getItemLayout if item heights vary [OK]
Common Mistakes:
Using index as key causing re-renders
Assuming fixed height for variable items
Rendering too many items initially
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.