React Native: Infinite Scroll with onEndReached
The onEndReached prop is a callback that fires when you scroll near a list's end, letting you fetch more data. Use it in FlatList for feeds or catalogs. The main footgun: it can fire multiple times, so use a loading flag to prevent redundant API.
Why it exists
Mobile apps often display lists with thousands of items. Loading all this data at once would consume huge amounts of memory and make the app slow to start. Infinite scrolling provides a better user experience by loading just enough data to fill the screen, then fetching more as the user scrolls down.
The mental model
Think of onEndReached as a sensor placed near the bottom of your list. When the last rendered item gets within a certain distance of the end of the viewport, this sensor fires a function you provide. Your function is responsible for fetching the next "page" of data from your API and appending it to the list's data source.
How it works
You pass a function to the onEndReached prop of a FlatList or SectionList. This function typically sets a loading state to true, makes an API call for the next page of data, and once the data arrives, appends it to the existing data array in your component's state. The onEndReachedThreshold prop, a number between 0 and 1, determines how close to the end (as a fraction of the list's visible length) the user must be for the callback to fire. A value of 0.5 means it triggers when the end of the content is within half the visible length of the list.
When to use it
Use onEndReached for any list where the dataset is too large to load at once. This is standard for social media feeds, product listings in an e-commerce app, search results, or long message histories. It's a core pattern for building performant, scalable lists in React Native.
When not to use it
Don't use it for short, finite lists where all data can be loaded upfront without a performance penalty, like a settings menu. If your list items have vastly different, unpredictable heights and performance is suffering, you might need a more complex virtualization strategy, though FlatList handles most cases well.
One canonical example
A common mistake is failing to prevent multiple fetches. The onEndReached callback can fire rapidly if the user scrolls near the end. To fix this, use a loading flag. Inside your onEndReached function, first check if a fetch is already in progress (e.g., if (loading) return;). If not, set loading to true, make your API call, and only set loading back to false after the new data has been successfully added to your state. This ensures you only fetch one page of data at a time.
Interview question
What is the main purpose of using a loading flag with `onEndReached` in a React Native `FlatList`?
- a.To ensure the `onEndReached` callback only fires once per scroll session.
- b.To adjust the `onEndReachedThreshold` dynamically based on network conditions.
- c.To prevent redundant API calls when the user scrolls quickly near the end of the list.Correct
- d.To control the visibility of a loading indicator while new data is being fetched.
Why? this is the answer
The card explicitly states that `onEndReached` can fire multiple times, and a loading flag is used to prevent redundant API calls. While a loading flag can also control indicator visibility, its primary role in this context is to manage the 'footgun' of multiple fetches.
Just read this? Test yourself on what you have been reading.
Read the original → archive.reactnative.dev
- #react native
- #mobile
- #performance
- #listview
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react native — each one lists the topics its interview covers.
See open roles