This component shows a list of items that loads more data automatically when you scroll to the bottom. It uses onEndReached to detect when to fetch more items, creating an endless scroll effect.
The main component is a FlatList that displays a vertical list of Text items. At the bottom, a loading spinner (ActivityIndicator) appears when new data is loading.
Render Trace - 3 Steps
Step 1: FlatList
Step 2: renderItem (Text)
Step 3: ListFooterComponent (ActivityIndicator)
State Change - Re-render
Trigger:User scrolls near the bottom of the list
Before
data contains initial items, loading is false
→
After
loading is true, new items appended to data after fetch, loading set to false
Re-renders:FlatList and its children rerender to show new items and update loading spinner
UI Quiz - 3 Questions
Test your understanding
What triggers the loading of more items in this infinite scroll?
AWhen the app starts only
BWhen the user scrolls near the bottom and onEndReached fires
CWhen the user taps an item
DWhen the user shakes the device
Key Insight
Infinite scrolling improves user experience by loading data in chunks as needed, reducing initial load time and memory use. Using FlatList's onEndReached is an efficient way to detect when to fetch more data automatically.
Practice
(1/5)
1.
What is the main purpose of the onEndReached prop in a React Native FlatList?
easy
A. To detect when the user scrolls near the end of the list to load more items
B. To refresh the list when the user pulls down
C. To set the initial number of items displayed
D. To style the list footer component
Solution
Step 1: Understand onEndReached role
This prop triggers a function when the user scrolls near the end of the list.
Step 2: Compare with other options
Refreshing is handled by onRefresh, initial items by initialNumToRender, and styling is unrelated.
Final Answer:
To detect when the user scrolls near the end of the list to load more items -> Option A
Quick Check:
onEndReached triggers loading more [OK]
Hint: Remember: onEndReached means near list end, load more [OK]
Common Mistakes:
Confusing onEndReached with pull-to-refresh
Thinking it controls initial render count
Assuming it styles the list footer
2.
Which of the following is the correct way to use onEndReached in a FlatList?
onEndReached expects a function, so we pass a function reference or an arrow function.
Step 2: Analyze options
onEndReached() ("onEndReached()") calls an undefined function immediately. loadMoreItems() ("loadMoreItems()") executes immediately on render. () => loadMoreItems ("() => loadMoreItems") returns the function but does not call it when triggered. loadMoreItems ("loadMoreItems") passes the correct function reference.
Final Answer:
loadMoreItems -> Option D
Quick Check:
Pass a function reference, not call it immediately [OK]
Hint: Pass function reference `loadMoreItems`, not `loadMoreItems()` [OK]
Common Mistakes:
Calling the function immediately instead of passing it
Passing undefined or wrong function name
Confusing function reference with function call
3.
Given this code snippet, what will happen when the user scrolls near the end of the list?
Assume the list is short and the user scrolls to the bottom.
medium
A. The keyExtractor will cause duplicate keys error
B. The list will never load more items because onEndReachedThreshold is too low
C. loadMore will be called repeatedly causing many state updates and possible performance issues
D. The renderItem function is missing a return statement
Solution
Step 1: Understand onEndReached behavior
When the list is short, scrolling near the end triggers loadMore repeatedly because the end is always near.
Step 2: Identify consequence
This causes many state updates rapidly, which can cause performance problems or infinite loops.
Final Answer:
loadMore will be called repeatedly causing many state updates and possible performance issues -> Option C
Quick Check:
Repeated onEndReached calls cause performance issues [OK]
Hint: Short lists cause repeated onEndReached calls without loading guard [OK]
Common Mistakes:
Ignoring repeated calls without loading state
Blaming keyExtractor or renderItem incorrectly
Thinking threshold prevents repeated calls
5.
You want to implement infinite scrolling with a loading spinner at the bottom. Which approach correctly prevents multiple loadMore calls while loading?