React Native List View : Pagination With FlatList – Building A Great Scrollable List In React Native
FlatList is is an interface used to render flat list, the component is provided by Facebook’s React Native Library
Handy features of FlatList are as follows
- It renders list of items if you just provide rendering View
- You can mention whether your list is horizontal or vertical, by default its vertical list view
- Configurable Callbacks provided
- Header / Footer / Separator are supported by FlatList
- Pull To Refresh is made simple with React Native Flat List View
- Pagination is made easy using React Native Flat List view
How To Use React Native Flat List Component: With Loading symbol in Footer
<FlatList refreshing={this.state.refreshing} onRefresh={true} onEndReachedThreshold={0.5} onEndReached={({ distanceFromEnd }) => { //Call API to fetch next page of data console.log('on end reached ', distanceFromEnd); }} ListFooterComponent={( <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}> <Spinner color='#6d4c41' /> <Text>Loading please wait...</Text> </View> )} data={this.props.messages} renderItem={this.notificationContent.bind(this)} keyExtractor={(item) => item._id} />
React Native Pull To Refresh Using Flat List
Pull to refresh is made easy by just mentioning onRefresh = true and we should also mention refreshing = true while fetching new data
Pagination Using Flat List In React Native
To achieve pagination mention onEndReachedThreshold some value between 0 – 1 and a callback for onEndReached should be provided where we have a logic to call API to fetch next page of data
Official documentation of React Native FlatList has many more functionalities listed.
Issues with React Native FlatList or onEndReached doesn’t get fired
Some times pagination for FlatList doesn’t work as expected, onEndReached Will never get fired, or some times it will be called only first time when list is loaded. Possible reason for this behaviour would be keeping FlatList inside native base Container or Content, Keep FlatList inside View to fix the issue.
Another possible solution to the issue of FlatList is changing onEndReachedThreshold value to a small number like 0.001.
Another Interesting solution which worked for me is giving height explicitly to the FlatList , this will call onEndReached callback
style={{ height: ( Dimensions.get(‘window’).height – 100 ) }}
Addressing The issue : React Native FlatList onEndReached called again and again
This can be addressed by ignoring the callback calls, this is can be done by using distanceFromEnd, if the value is +ve then do your pagination stuffs, if the value is -ve then ignore the callback call. ReactNative FlatList returns +ve value when scroll reaches the end of the list. So the update code looks like below
<FlatList refreshing={this.state.refreshing} onRefresh={true} onEndReachedThreshold={0.5} onEndReached={({ distanceFromEnd }) => { if (distanceFromEnd >= 0) { //call API to get next page values } console.log('on end reached ', distanceFromEnd); }} data={this.props.messages} renderItem={this.notificationContent.bind(this)} keyExtractor={(item) => item._id} />