React Native Infinite List FlatList Pagination to Load More JSON Data dynamically on Button Click Tutorial

The Infinite List is a type of ListView – FlatList used to show only fixed number of data on application startup time and user can himself Load More Data if required on Button Click event. The Infinite List can show hundreds of records without lagging and application hanging process because of its quick way process method. It only loads fixed number of data and itself pass a paging parameter with HTTP URL so by using the application developers can tell the server to load new data and the data present on PHP MySQL server. So in this tutorial we would going to implement Infinite List FlatList Pagination to Load More JSON Data dynamically on Button Click Example Tutorial.

Live Screenshot of App:


Contents in this project Infinite List FlatList Pagination to Load More JSON Data dynamically on Button Click Tutorial in React Native:

1. Create new database on your MySQL PhpMyAdmin panel.

2. Create a new table named as Chart with 2 columns id and value. The id is auto increment and as declared as primary key and value is varchar type. Now we would store one to hundred table inside the MySQL database table. After inserting records in table we have 100 records inside the table, Below is the screenshot of table.

3. Creating PHP script to convert MySQL data into JSON :

We are using PHP to convert MySQL database data into JSON form, We would use the Get method of PHP and split all the data into multiple segments(pages), so we have to pass the page number along with the URL and it will show record according to that URL. There is a fixed condition in PHP Code though it will only show 10 Records at a single time.

DBConfig.php file : This file contain all the useful information about your server.

counting_table.php : This file would convert the MySQL data into JSON and show only 10 records at a single time. User need to pass the page number along with URL. The URL of this page should look like this:

We need to pass the Page number with URL so it will divide the records and show filtered records according to give page number.

Code for DBConfig.php file : 

Code for counting_table.php file:

Screenshot of JSON :

4. Start a new react native project, If you don’t know how to then read my this tutorial.

5. Import StyleSheet, View, Text, Platform, FlatList, TouchableOpacity and ActivityIndicator component in your project.

5. Create constructor() in your class, Now we would make 3 states named as isLoading, JSON_from_server and fetching_Status inside constructor(). We would also make a this variable named as this.page.

isLoading : This state is used to show and hide the ActivityIndicator when application start first time.

JSON_from_server : This is a State array used to store the JSON data coming from server.

fetching_Status : This state is used to show and hide the ActivityIndicator which shows inside the Loading Button.

this.page : This is a page parameter pass along with URL.

6. Create componentDidMount() method in your class, This is a inbuilt method and calls every time when application startup. We would call the server URL along with page number inside this function and parse the JSON.

7. Create fetch_more_data_from_server() function in your class, Inside this function we would again request for Web call and call the next 10 items from server by passing the Page number along with URL.

8. Create another function named as c, this function would render a horiontal Separator line between each FlatList item.

9. Create a function named as Render_Footer(), this function is used to render a View at the bottom of FlatList. Inside this view we would make a TouchableOpacity button which shows at the last of FlatList with Loading indicator.

10. Create a Root View inside render’s return block. Now inside this View we would make the FlatList component.

keyExtractor : Extract the unique key from FlatList.

data : Setting up the JSON data into FlatList.

ItemSeparatorComponent : Calling the ItemSeparatorComponent() function to render the separator line.

renderItem : Rendering the JSON items.

ListFooterComponent : Showing the footer view.

11. Create Style.

Screenshots in Android device:

Infinite List

Screenshots in iOS device: