React-native pagination example – infinity list

In this article, we learn about React-native pagination example – infinity list, to explain this we use react-native Flatlist component in which we will render data in the form of pages or offset and limit etc, to do this

I have reacted API for pagination and will write a different post for APIs, in this example we will going to call that API call which returns infinite list so let’s start,

Import following component

Write a constructor like below

See above constructor, we define state variable this like isLoading which indicate fetch API call is calling, responseList which type array which store fetch call response list, fetchingStatus only indicates for onLoadMore call fetching, this.page = 0  is the default value of the page which we want to fetch during the 1st API call

Create a method for API call

While creating a function, it’s good to create arrow function in this situation because we have to call the same function multiple times and arrow function does not create every render, so it’s good to use arrow function like below

define a variable that and use that variable inside this method, also at the beginning of function I am adding page + 1 so that on the second API call it will get second (i.e. previous page count + 1)

after that call our standard fetch method like below:

If you want to learn more about Fetch API call then I request you to read our Post related Fetch GET API call

Fetch Get API call:

after success, response save response list in state variable responseList we are not saving new data each time

we are adding a new response in previously saved list, after that setting setOnLoad: true which means that we can call next API onLoadMore, the question is how to load data onLoadmore to do this Flatlist has inbuild properties like onEndReached which call when list reach ends, let’s understand using code see below:

Render data using FlatList

In about code, I use FlatList to render data and I added one property called onEndReached which called when all rows have been rendered and the FlatList has been scrolled to within onEndReachedThreshold of the bottom.

Value 0.5 for onEndReachedThreshold means, when the FlatLast row item is reached within half of a screen-full of items from being visible

I added one property ListFooterComponent which show Footer view on FlatList we will display loader in this section let’s create a loader

This is display loader when onLoadmore API call is fetching data

Here is the full code

My App.js

You can also create another js file for this, I have added whole code in App.js

 

React-native pagination example using FlatList
React-native pagination example using FlatList

 

Thank you 🙂

2 Replies to “React-native pagination example – infinity list”

Leave a Reply

Your email address will not be published. Required fields are marked *