Challenge - 5 Problems
Infinite Scrolling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when onEndReached triggers in this FlatList?
Consider this React Native FlatList component with an onEndReached handler that fetches more items. What will the user see after scrolling to the bottom once?
React Native
import React, { useState } from 'react'; import { FlatList, Text, View } from 'react-native'; export default function App() { const [items, setItems] = useState(Array.from({ length: 20 }, (_, i) => i + 1)); const loadMore = () => { const newItems = Array.from({ length: 10 }, (_, i) => items.length + i + 1); setItems([...items, ...newItems]); }; return ( <FlatList data={items} keyExtractor={item => item.toString()} renderItem={({ item }) => <Text>{`Item ${item}`}</Text>} onEndReached={loadMore} onEndReachedThreshold={0.5} /> ); }
Attempts:
2 left
💡 Hint
Think about what the loadMore function does when called.
✗ Incorrect
The onEndReached triggers when the user scrolls near the bottom. The loadMore function adds 10 new items to the existing list, so the list grows and the user can scroll further.
❓ lifecycle
intermediate2:00remaining
Why might onEndReached fire multiple times rapidly in this example?
Look at this FlatList with onEndReached calling fetchMore. Why could the fetchMore function be called many times quickly when scrolling to the bottom?
React Native
import React, { useState } from 'react'; import { FlatList, Text } from 'react-native'; export default function App() { const [data, setData] = useState(Array.from({ length: 15 }, (_, i) => i + 1)); const fetchMore = () => { const more = Array.from({ length: 5 }, (_, i) => data.length + i + 1); setData([...data, ...more]); }; return ( <FlatList data={data} keyExtractor={item => item.toString()} renderItem={({ item }) => <Text>{`Item ${item}`}</Text>} onEndReached={fetchMore} onEndReachedThreshold={0.1} /> ); }
Attempts:
2 left
💡 Hint
Think about what happens when state updates inside onEndReached.
✗ Incorrect
When fetchMore updates the data state, the FlatList re-renders and the scroll position is still near the bottom, causing onEndReached to fire again immediately. This can cause multiple rapid calls.
🔧 Debug
advanced2:00remaining
What error occurs with this onEndReached handler?
This FlatList's onEndReached handler tries to fetch more data but causes an error. What error will this code produce?
React Native
import React, { useState } from 'react'; import { FlatList, Text } from 'react-native'; export default function App() { const [items, setItems] = useState([1, 2, 3]); const loadMore = () => { setItems(items.push(items.length + 1)); }; return ( <FlatList data={items} keyExtractor={item => item.toString()} renderItem={({ item }) => <Text>{`Item ${item}`}</Text>} onEndReached={loadMore} onEndReachedThreshold={0.5} /> ); }
Attempts:
2 left
💡 Hint
Check what Array.push returns and what setItems expects.
✗ Incorrect
Array.push returns the new length of the array, not the array itself. So setItems is called with a number, causing a TypeError because data must be an array.
🧠 Conceptual
advanced2:00remaining
Which option best prevents multiple onEndReached calls during loading?
You want to avoid multiple fetches while loading more data in onEndReached. Which approach below correctly prevents multiple calls?
Attempts:
2 left
💡 Hint
Think about how to track if a fetch is in progress inside the component.
✗ Incorrect
Using a loading boolean state inside the component to block fetchMore calls while loading is the correct way to prevent multiple simultaneous fetches.
expert
2:00remaining
What is the best way to reset FlatList scroll position after loading new data?
After loading more items in onEndReached, you want to reset the FlatList scroll to the top. Which method correctly achieves this?
React Native
import React, { useState, useRef } from 'react'; import { FlatList, Text, Button } from 'react-native'; export default function App() { const [data, setData] = useState(Array.from({ length: 20 }, (_, i) => i + 1)); const listRef = useRef(null); const loadMore = () => { const more = Array.from({ length: 10 }, (_, i) => data.length + i + 1); setData([...data, ...more]); // Reset scroll here }; return ( <> <Button title="Load More and Scroll Top" onPress={loadMore} /> <FlatList ref={listRef} data={data} keyExtractor={item => item.toString()} renderItem={({ item }) => <Text>{`Item ${item}`}</Text>} /> </> ); }
Attempts:
2 left
💡 Hint
Think about how to scroll programmatically after state updates.
✗ Incorrect
Calling scrollToOffset with offset 0 after updating data scrolls the list to the top. Doing it before setData or using scrollToEnd won't reset to top.