Under the hood
Last year, I was working on a cryptocurrency-based startup. we have developed a mobile application that was serving almost 5 Lakhs users, it was going good until a few comments on the google play store start getting more shares and likes. That comment was bashing our application because of his performance, our application in few devices specially older ones is lag a lot or even gets shut down in some cases. We have to now address this problem as the fast-paced startups losing customers on daily basis is like doing reverse counting to the number of days left to your startup.
Problem
We have to first encounter the core of the problem, for example,
- Which page is quite slow
- Which devices are more prone to this slowness
- Which component of that page is creating this lag
- If not, then does API calling or Redux is making our component slow and so on.
After, reading tonnes of articles and applying brute force as above steps, we have reached this conclusion that may be our list of data was rerendering and making that single page a bit slow. Of course, this was not the only reason why our users start bashing our application on Play Store. We have found a bunch of other reason too in terms of why our application was a bit slow. But so far using a list with unnecessary rerendering causes an application a bit slowness.
Addressing the issue
The main problem was with the list, we have a list with dynamic items, meaning the number of items to each list was not fixed, it keeps on increasing or decreasing with time. We have used the simple ScrollView component of react native and as most of us do, we return every item of the list using the map function inside this scroll view component.
Problem with ScrollView
The main problem with having ScrollView and map function with this logic the scroll view rendered the whole list in one single mount. If you 20 or 100 items in the list the scroll view will render all of them at the same time, irrespective of the fact that the items hiding below the screen will only be required when those items come into the view. Also, the ScrollView component does not provide any lazy loading to the items. Yes, you can add lazy loading on ScrollView on your own but maintaining it will be costly.
Solution
Flatlist is the most underrated component in my opinion. Most of the developers go with simple ScrollView to show the list.
When I asked the developers, Why they prefer ScrollView most of the time?. They answered that Flatlist can’t be used with ScrollView and what if you single page has both list of element and scroll property than you can’t use Flatlist and ScrollView. So they always end up using ScrollView.
Yes, they were right, but half knowledge is dangerous as we all know, I read a bit more about it, I have one single question why Flatlist is still exist if ScrollView is used by almost all of them. Fortunately, I have found a bunch of react-native own documentation about Flatlist and its importance as follows-
- Flatlist supports lazy loading,
- Flatlist provide on scroll pagination,
- Flatlist avoid unnecessary rerendering,
- Flatlist scrolls are fast and page rendering time decreases once used.
- And Yes, Flatlist can be used with scroll property
Implementation
The agenda is to use flatlist with scroll property, tonnes of items and lazy loading and on scroll pagination. For sample data, I will be using a list of todos of 100 items, the API link is — https://jsonplaceholder.typicode.com/todos
- Add header and Footer to Flatlist which support scroll property
- After every 10 items, we will add a loader that will fetch 10 more items and show them accordingly.
- Only 10 items appeared initially.
- Every item is a single pure component that will avoid unnecessary re-rendering.
Writing Code
I will use using the same repository, I have created for adding maps in the react-native app.
Flatlist provide the most important and underrated props defined as follows-
- ListHeaderComponent — By the virtue of name will add a Header to Flatlist so you don't need to scroll view to wrap around Flatlist.
- ListFooterComponent — Anything after the list can be added as ListFooterComponent.
- onRefresh — On pull to refresh property, which can be used to make API calls to fetch updated data.
- onEndReached — A method that gets called when the user reaches the end item of Flatlist
- initialNumToRender — Takes the initial number of items the list will be rendered initially.
- onEndReachedThreshold — This value decides at what scroll height ratio to call the onEndReached method, for example, its value is set to 0.5 then once the user scrolled to half the height of the screen the onEndReached function is called.
Those above props are least heard and expected, we too have missed it initially. I will be using all of them to explain how to execute a perfect Flatlist.
We will fetch the data using axios from the endpoint. Fetched data then split into chunks of 10 new arrays like paginated data. A list will show 10 items and once the user reaches the end item or 10th, 20th or 30th and so on the succeeding 10 items were called and rendered altogether. This process of lazy loading helps Flatlist to render fast initially.
Also, the single item of the array is a Pure component that will not re-render unnecessary and rerendering occurs only when required. You can also create every single item as a memoized item that follows the concept of tree shaking. This means the particular component will get render only when its corresponding states or values changes hence it will not even when its immediate parent re-renders or not.
As soon as the user reaches the end the prop onEndReached gets executed and we will increase the data size of Flatlist and this will render the succeeding item.
We have also added the loader for user feedback about fetching data for users. This provides a better user experience by informing the user about fetching more data once the user reaches the end.
Below down is my SingleItem Pure component of Flatlist.
SingleItem.jsx
My SingleItem component is Pure Component which avoids unnecessary re-rendering and helps Flatlist to load fast.
Fetching next 10 items
See the concept is a little tricky behind fetching the next 10 items once the user scrolled. There are 2 ways to do that as follows-
- Fetch only the 10 items from a database and once the user scrolled down fetch more than 10 items and append them to corresponding data.
- Fetch all the items once and split them according to the index of scroll value, if the user scroll and reached to end for 3rd time then the current index will be 3 and we need to fetch items from 21 to 30 index from the complete data.
The first way was quite simple and easy to execute over the second way. The API we used was created using JSON-Server and the npm package was used to create Sample API with all HTTP methods. I have also covered an article regarding JSON-Server.
Creating a fake Node.js server
This API supports pagination too and we can provide a _page as a query and its value will be that particular page value. We have set onEndReachedThreshold to 0.7 and inside the onEndReached function, I am updating the page value and fetching the succeeding 10 items from API.
The Flatlist component consists of Loading… as ListFooterComponent, this will only be visible once the user reaches the end via scrolling and simultaneously we were fetching data from API so it's always a good idea to show loader or loading text to the user in the ListFooterComponent.
We have added all the required features of Flatlist to improve a list performance with lazy loading and avoid unnecessary re-rendering. This approach hikes up the performance in comparison to ScrollView with large sets of the list.
Final Product
Here is what my Final Product looks like.

Final Product View
Conclusion
I am in love with Flatlist for so long, today I have covered one article on its implementation. Stay tuned because I will be covering a new tech log every day.
Code => https://github.com/shreyvijayvargiya/iHateReadingLogs/tree/main/TechLogsMobile/FlatlistInReactNativeUntil, next time. Have a good day, People.
React NativeReactReactjs
