What we’ll build
We’ll build an infinite scrolling user listing application using the React Native FlatList component.
Prerequisites
To follow along in this post, here are a few prerequisites to note:
Getting started
We’re going to start with a few installations for those using React Native for the first time. If this is your first time working with React Native, be sure to install the required packages and follow these steps accordingly:
1. Install the React Native CLI tool using npm like so:
Afterwards, go ahead and run these React Native commands to create and start a new project.
2. Create the animations project and start the server:
If you have Xcode or Android Studio installed then the commands above will run the project and you should have the simulator show up on the screen like this:
Install dependencies
Before we start, It’s important to first install the necessary dependencies for this project, in our case, we only need: Axios — for making API requests to fetch users and React Native Elements — for the custom card components we’ll use in this project.
Set up the Users component
In the root directory of the sample project we created, create a new src/components/Users.js file. In this file, first, we fetch the users that we want to display here from the Github Users API. To do that, we define a fetchUsers() function that will make a call to the API and initially get us a list of 10 users:
Here, we first defined three state properties, users to hold the users data fetched from the API, page to denote which page of the users API we are fetching, and error to handle error cases.
Then we defined a fetchUsers() function that uses Axios to fetch users and update our users array with the fetched data. Finally, we want to call this function when our app is mounted so let’s do that with the componentDidMount() lifecycle method:
Now when the app loads, we’ll have the users fetched in our application, we can go ahead and update the app UI with the data.
Rendering The Fetched User
React Native provides us with a <FlatList/> UI component that handles the rendering of a simple flat list such as this one, in a performant way. Here, we’ll use it to display the list of users.
The <FlatList/> component provides us with a good number of props through which we customise its behaviour to suit our needs. The two major props that are always required are the data and the renderItem props.
We use the data prop to supply the data content we intend to render in the
FlatList, and then use the renderItem prop to de-structure the data and render each item. In due time, we’ll use other features of the <FlatList/> component to implement infinite scrolling feature that allows user to scroll through several pages of this application.
Run The App
Save the current changes and run the app with any of the commands below and we’ll get the following visual output:
Infinite Scroll
Now that we have the initial functionality set up, it’s time to implement the infinite scroll feature. Thankfully, the <FlatList/> component also gives us the ability to do this. Among its other features, Scroll loading is one performant feature that comes with the <FlatList/> component. It makes it possible for us to load more content on-demand.
In our case, we will automate the demand such that whenever a user scrolls down to a certain extent in our app, we’ll request for more content. This will be possible using the FlatList components’ onEndReached prop and also the onEndReachedThreshold prop.
FlatList Scroll Props
The onEndReached prop is a callback function that fires when the onEndReachedThreshold is reached. The onEndReachedThreshold prop is a number that indicates at which position of the page the list will be at for the onEndReached callback to fire.
Basically what this means is that we use the onEndReachedThreshold to specify a value that will correspond with the content of the page, when the user scrolls to that value, we trigger the onEndReached callback and fetch more content, hence, infinite scroll.
Next, define the function that gets called when the set threshold is reached. Call it fetchMoreUsers(), that’s exactly what it’ll do:
Here, we update the value of the page variable to fetch a different page of users from the API and finally, we call the fetchUsers() function. This will then fetch the new page of users and render them on our FlatList. How do we tell our application to call the fetchMoreUsers() function? You probably guessed right, the onEndReached prop. Let’s pass in those new props to our <FlatList /> component.
This way, when a user scrolls down half the current page, the fetchMoreUsers() function is called to load more content, hence, infinite scrolling achieved. Now if we run the app again, we get the infinite scrolling ability:
More Performance Optimizations
If you’re dealing with a large list that may often take time to load or update in a virtualized list component (FlatList) It may have some performance costs since the component will update on every state change of any component. The advised approach to handling such a situation is to either use a PureComponent or use the shouldComponentUpdate() lifecycle method.
Conclusion
Infinite scrolling is a not so popular feature in both web and mobile applications with great performance and usability gains. In this project, we have demonstrated how to implement it using the FlatList component in a React Native project to give you more practical experience of using this feature. You can learn more about FlatList in the documentation. To dig the source, find the project repository here.Infinite scrolling is a very important piece of functionality in modern mobile applications. When done right, it improves the performance of the application, optimizes render calls and offers an overall great user experience.
Why? because it makes loading a predefined amount of content possible on page load, and only fetching more content on-demand. In this post, we’ll show you how to implement this functionality while building a React Native app. Learn more here.
Automated E2E tests for your mobile app
Get true E2E testing in minutes, not months.