Bird
Raised Fist0
React Nativemobile~20 mins

FlatList performance optimization in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Optimized FlatList Screen
A screen that shows a long list of items using FlatList with performance improvements.
Target UI
-------------------------
| Optimized FlatList     |
|-----------------------|
| Item 1                |
| Item 2                |
| Item 3                |
| ...                   |
| Item 1000             |
-------------------------
Use FlatList to display 1000 items labeled 'Item 1' to 'Item 1000'.
Implement keyExtractor for stable keys.
Use getItemLayout to optimize scroll performance.
Use initialNumToRender to limit initial rendering.
Use removeClippedSubviews to improve memory usage.
Avoid inline functions in renderItem.
Add simple styling for readability.
Starter Code
React Native
import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';

export default function OptimizedFlatList() {
  const data = Array.from({ length: 1000 }, (_, i) => ({ id: String(i + 1), title: `Item ${i + 1}` }));

  // TODO: Implement optimized FlatList here
  return (
    <View style={styles.container}>
      <FlatList
        data={data}
        renderItem={({ item }) => <Text style={styles.item}>{item.title}</Text>}
        keyExtractor={item => item.id}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, paddingTop: 40 },
  item: { padding: 15, borderBottomWidth: 1, borderColor: '#ccc' },
});
Task 1
Task 2
Task 3
Task 4
Solution
React Native
import React, { useCallback } from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';

const ITEM_HEIGHT = 50;

const data = Array.from({ length: 1000 }, (_, i) => ({ id: String(i + 1), title: `Item ${i + 1}` }));

export default function OptimizedFlatList() {
  const renderItem = useCallback(({ item }) => {
    return <Text style={styles.item}>{item.title}</Text>;
  }, []);

  const getItemLayout = (_data, index) => ({ length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index });

  return (
    <View style={styles.container}>
      <FlatList
        data={data}
        renderItem={renderItem}
        keyExtractor={item => item.id}
        initialNumToRender={10}
        getItemLayout={getItemLayout}
        removeClippedSubviews={true}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, paddingTop: 40 },
  item: { height: ITEM_HEIGHT, paddingHorizontal: 15, justifyContent: 'center', borderBottomWidth: 1, borderColor: '#ccc' },
});

We created a constant ITEM_HEIGHT to define the height of each item. This helps getItemLayout calculate the position of each item, which improves scroll performance by avoiding measurement on the fly.

We used useCallback to memoize the renderItem function, preventing unnecessary re-renders.

Setting initialNumToRender to 10 limits how many items render initially, speeding up the first load.

removeClippedSubviews frees memory by unmounting items outside the viewport.

These combined optimizations make the FlatList smooth and efficient even with 1000 items.

Final Result
Completed Screen
-------------------------
| Optimized FlatList     |
|-----------------------|
| Item 1                |
| Item 2                |
| Item 3                |
| ...                   |
| Item 1000             |
-------------------------
User can scroll smoothly through 1000 items.
Items load quickly without lag.
Memory usage stays low due to clipping.
Stretch Goal
Add a search bar to filter the list items by their title.
💡 Hint
Use a TextInput above the FlatList and filter the data array based on the input text, updating the FlatList's data prop.

Practice

(1/5)
1. What is the main purpose of using FlatList in React Native?
easy
A. To efficiently render long lists by only rendering visible items
B. To create complex animations for list items
C. To handle user authentication in the app
D. To manage app navigation between screens

Solution

  1. Step 1: Understand FlatList's role

    FlatList is designed to render lists efficiently by only rendering items visible on the screen.
  2. Step 2: Compare options

    The other options describe unrelated features like animations, authentication, and navigation.
  3. Final Answer:

    To efficiently render long lists by only rendering visible items -> Option A
  4. Quick Check:

    FlatList optimizes rendering = A [OK]
Hint: FlatList renders only visible items for performance [OK]
Common Mistakes:
  • Thinking FlatList handles navigation
  • Confusing FlatList with animation libraries
  • Assuming FlatList manages user login
2. Which of the following is the correct way to provide a fixed height for each item in a FlatList using getItemLayout?
easy
A. getItemLayout={(data, index) => ({length: index, offset: 50, index})}
B. getItemLayout={(data, index) => ({length: 50, offset: index, index})}
C. getItemLayout={(data, index) => ({length: 50, offset: 50 * index, index})}
D. getItemLayout={(data, index) => ({length: 50, offset: 50 + index, index})}

Solution

  1. Step 1: Understand getItemLayout parameters

    getItemLayout expects an object with length (item size), offset (distance from start), and index.
  2. Step 2: Calculate offset correctly

    Offset should be item height multiplied by index to position items correctly.
  3. Final Answer:

    getItemLayout={(data, index) => ({length: 50, offset: 50 * index, index})} -> Option C
  4. Quick Check:

    Offset = item height * index = B [OK]
Hint: Offset equals item size times index in getItemLayout [OK]
Common Mistakes:
  • Using index as offset directly
  • Adding index to offset instead of multiplying
  • Swapping length and offset values
3. Given this FlatList snippet, what will be the initial number of items rendered on screen?
  <FlatList
    data={data}
    renderItem={({item}) => <Text>{item}</Text>}
    initialNumToRender={5}
  />
medium
A. 5 items
B. All items in data
C. 10 items
D. No items until scrolled

Solution

  1. Step 1: Understand initialNumToRender prop

    This prop controls how many items FlatList renders initially on screen.
  2. Step 2: Check the value given

    initialNumToRender is set to 5, so FlatList renders 5 items first.
  3. Final Answer:

    5 items -> Option A
  4. Quick Check:

    initialNumToRender = 5 means 5 items rendered initially [OK]
Hint: initialNumToRender sets initial visible items count [OK]
Common Mistakes:
  • Assuming all items render at once
  • Confusing initialNumToRender with maxToRenderPerBatch
  • Thinking no items render until scroll
4. Identify the error in this FlatList usage that causes slow scrolling:
  <FlatList
    data={items}
    renderItem={({item}) => <Text>{item.name}</Text>}
    keyExtractor={(item) => item.id}
    removeClippedSubviews={false}
  />
medium
A. renderItem should return a View, not Text
B. keyExtractor should return index instead of item.id
C. data prop should be an object, not an array
D. removeClippedSubviews should be true to improve performance

Solution

  1. Step 1: Understand removeClippedSubviews effect

    Setting removeClippedSubviews to true helps FlatList remove off-screen items from memory, improving scroll performance.
  2. Step 2: Analyze current setting

    Currently, removeClippedSubviews is false, which can cause slow scrolling due to extra memory use.
  3. Final Answer:

    removeClippedSubviews should be true to improve performance -> Option D
  4. Quick Check:

    removeClippedSubviews true improves scroll speed [OK]
Hint: Enable removeClippedSubviews for better scroll performance [OK]
Common Mistakes:
  • Ignoring removeClippedSubviews prop
  • Using index as keyExtractor causing re-renders
  • Thinking renderItem must return View only
5. You have a FlatList with 1000 items of fixed height 60. To optimize scrolling performance, which combination of props should you use?
hard
A. initialNumToRender=50, maxToRenderPerBatch=50, windowSize=10, getItemLayout missing
B. initialNumToRender=10, maxToRenderPerBatch=10, windowSize=5, getItemLayout provided
C. initialNumToRender=1, maxToRenderPerBatch=1, windowSize=1, getItemLayout missing
D. initialNumToRender=1000, maxToRenderPerBatch=1000, windowSize=20, no getItemLayout

Solution

  1. Step 1: Use getItemLayout for fixed height items

    Providing getItemLayout lets FlatList calculate item positions quickly, improving scroll speed.
  2. Step 2: Choose reasonable rendering batch sizes

    initialNumToRender=10 and maxToRenderPerBatch=10 balance initial load and batch rendering without overloading memory.
  3. Step 3: Set windowSize to control offscreen rendering

    windowSize=5 keeps a small buffer of items rendered outside viewport for smooth scrolling.
  4. Final Answer:

    initialNumToRender=10, maxToRenderPerBatch=10, windowSize=5, getItemLayout provided -> Option B
  5. Quick Check:

    Balanced props + getItemLayout = D [OK]
Hint: Use getItemLayout with balanced batch sizes for big lists [OK]
Common Mistakes:
  • Rendering all items at once causing lag
  • Missing getItemLayout for fixed height items
  • Setting windowSize too large or too small
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard