Recognized by Clutch.co as a top-rated Mobile App Development Company.
folio3-mobile
US 408 365 4638
START YOUR PROJECT
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us
Menu
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us

What is Flatlist and how to use Flatlist in React Native

Published by: Nimra Saad | November 9, 2020 Taimoor Malik
SCROLL AND BE AMAZED!
Home > React Native > What is Flatlist and how to use Flatlist in React Native

React native is the most evolving technology nowadays. It provides many built-in components. One of the important and useful components is Flatlist.

Flatlist:

Flatlist is the easiest way to render a scrollable list of items. We just have to pass the data (as an array) without any formatting to it and it will render the items. Flatlist works efficiently with large data as it renders only those items that are displaying on the screen, and not all the items at once.

Basic Props:

The list can be rendered, horizontally and vertically with custom header, footer, and separator. The basic props of flatlist are:

  • data → an array of objects (items) to be rendered
  • renderItem → iterates over data and renders items
  • horizontal → by default, list is rendered vertically. If this property is set to true, the list will render horizontally.
  • extraData → if list is dependent on any property other than data prop then mention the property here. The list will re-render itself when this property will change.
  • keyExtractor → specifies which property of object should be used as key
  • ItemSeparatorComponent → renders component in between item as a separator, not on top (start) and bottom (end) of the list
  • ListHeaderComponent → renders custom header component
  • ListFooterComponent → renders custom footer component
  • ListEmptyComponent → renders empty listview

Example:

Here, below is a simple example of usage of flatlist in react native application which displays a list of employees in a software house.

import React, {Component} from 'react';
import {View, Text, FlatList, StyleSheet, TouchableOpacity} from 'react-native';
 
export default class FlatListComponent extends Component {
 constructor(props) {
   super(props);
 
   this.state = {
     refresh: false,
   };
 }
 
 componentDidMount() {
   // this.makeRemoteRequest();
 }
 
 renderHeader = () => {
   return (
     <View>
       <Text style={styles.header}>Employees</Text>
     </View>
   );
 };
 
 renderFooter = () => {
   return (
     <View>
       <Text style={styles.footer}>End</Text>
     </View>
   );
 };
 
 emptyListView = () => {
   return (
     <View>
       <Text>No records found.</Text>
     </View>
   );
 };
 
 renderSeparator = () => {
   return <View style={styles.itemSeparator}></View>;
 };
 
 onItemSelect = item => {
   console.log('item', item);
 };
 
 render() {
   let data = [
     { id: 1, name: 'John Brahm', designation: 'Project Manager' },
     { id: 2, name: 'Tom Jack', designation: 'Software Engineer' },
     { id: 3, name: 'Mark Bell', designation: 'QA Engineer' },
     { id: 4, name: 'Marshall Doe', designation: 'Software Engineer' },
     { id: 5, name: 'John Dow', designation: 'Product Manager' },
     { id: 6, name: 'Harry Jam', designation: 'Team Lead' },
     { id: 7, name: 'Oliver James', designation: 'Graphic Designer' },
     { id: 8, name: 'Ella Avery', designation: 'QA Engineer' },
     { id: 9, name: 'William Thomas', designation: 'Graphic Designer' },
     { id: 10, name: 'Edward Brian', designation: 'Team Lead' },
   ];
   return (
     <View style={styles.container}>
       <FlatList
         data={data}
         extraData={this.state}
         keyExtractor={item => item.id}
         ListHeaderComponent={this.renderHeader}
         ListFooterComponent={this.renderFooter}
         ListEmptyComponent={this.emptyListView}
         ItemSeparatorComponent={this.renderSeparator}
         renderItem={({item}) => {
           return (
             <TouchableOpacity
               onPress={() => {
                 this.onItemSelect(item);
               }}>
               <View style={styles.flatlist}>
                 <Text style={styles.heading2}>{item.name}</Text>
                 <Text style={styles.subheading}>{item.designation}</Text>
               </View>
             </TouchableOpacity>
           );
         }}
       />
     </View>
   );
 }
}
 
const styles = StyleSheet.create({
 container: {
   flex: 1,
 },
 header: {
   fontSize: 30,
   paddingVertical: 15,
   fontWeight: 'bold',
   textAlign: 'center',
   backgroundColor: '#DCDCDC',
 },
 footer: {
   fontSize: 30,
   paddingVertical: 15,
   fontWeight: 'bold',
   textAlign: 'center',
   backgroundColor: '#DCDCDC',
 },
 flatlist: {
   paddingVertical: 30,
   paddingHorizontal: 10,
 },
 heading2: {
   fontSize: 18,
 },
 subheading: {
   color: 'grey',
 },
 itemSeparator: {
   backgroundColor: 'green',
   height: 1,
 },
});

Output:

Pull to Refresh:

This feature is very important if we are going to work with lists. Users can see the latest data in the list by remaining on the same view. We have to add a refreshControl prop to the Flatlist component which calls the custom function to refresh the list. Make sure if you have not used Flatlist within ScrollView directly otherwise refreshControl will not work.

refreshControl={
           <RefreshControl
             refreshing={this.state.refreshing}
             onRefresh={() => this.onRefreshList()}
             title={'Fetching data...'}
           />
         }

And we have to create a callback function onRefreshList that will be called on the Pull to Refresh request.

onRefreshList = () => {
   this.setState({refreshing: true});
   //server call to update data object - here we are using hardcoded data
   let updatedData = this.state.data;
   updatedData.push({ id: 11, name: 'Marsh Ken', designation: 'Graphic Designer'});
   this.setState({
     refreshing: false,
     data: updatedData
   })
 };

Output:


Avatar
About Nimra Saad

Nimrah saad is working as a Senior Software Engineer at Folio3, working as a Full-stack developer for the last four years. She has core expertise in JavaScript frameworks, Angular, React, and hands-on experience with mobile technologies, Ionic and React Native. She also focuses on client-side frameworks, and expertise in server-side languages, NodeJS, and PHP. She enjoys reading, learning, and experimenting with new challenges; she is an enthusiastic individual who remains motivated towards her goal and believes in encouraging others.

Newsletter

Search

Archives

  • December 2020
  • November 2020
  • October 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • Categories

    • Android App Development
    • App Development
    • App Testing
    • Blog
    • Elasticsearch
    • flutter-app-development
    • IOT
    • React Native
    • Staff Augmentation

Recent Posts

  • Startup Incubator – How to Make an App Start on Startup?
  • Jquery Vs. React: Which One Is The Best Option To Create An App For Business?
  • One codebase to rule them all – Sharing code in mobile & web apps using Flutter
  • What is Tree Shaking and Implementation in React
  • Vue VS. React – Crowning the King of Web App Development in 2021

Tags

  • android
  • Automation
  • cross-platform
  • development
  • firebase
  • ios
  • QA
  • react-native
  • Testing
  • Test Script

Newsletter

Newsletter

Post navigation

Previous Working with Multiple Environments in iOS/React-Native using Schemes
Next Private Equity vs. Venture Capital: What You Need to Know about App Funding

One thought on “What is Flatlist and how to use Flatlist in React Native”

  1. Avatar Taimur khan says:
    November 12, 2020 at 3:19 pm

    What about Nativescript? 😉

    Reply

Leave a Reply Cancel reply

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

Schedule an appointment with our Mobile App Development Expert

Footer Menu
  • Company
    • About Us
    • Portfolio
    • Blog
    • Careers
    • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise App Development
    • AR/VR Application Development
    • IoT Application Development
    • Wearables Apps Development
    • Field Sales
    • On-Demand Apps Development
  • Technologies
    • iOS
    • Android
    • React Native
    • Flutter
    • Ionic
    • Xamarin
    • NativeScript
    • HTML5
    • Sencha
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical
    • Manufacturing
    • Automotive
    • Logistics
    • Education

US Office

Belmont, California – 1301 Shoreway Road, Suite 160, Belmont, CA 94002

Pleasanton, California – 6701 Koll Center Parkway, #250 Pleasanton, CA 94566

Tel: +1 408 365 4638
Support: +1 (408) 512 1812

Mexico Office

Amado Nervo #2200, Edificio Esfera 1 piso 4, Col. Jardines del Sol, CP. 45050, Zapopan, Jalisco, Mexico

Bulgaria Office

49 Bacho Kiro Street, Sofia, 1000, Bulgaria

Canada Office​

895 Don Mills Road, Two Morneau Shepell Centre, Suite 900, Toronto, Ontario, M3C 1W3, Canada

UK Office

Export House, Cawsey Way, Woking Surrey, GU21 6QX

Tel: +44 (0) 14 8361 6611

UAE Office

Dubai, UAE – Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

Tel: +971-55-6540154
Tel: +971-04-2505173

Pakistan Office

163 Bangalore Town, Main Shahrah-e-Faisal, Karachi –
75350

705, Business Center, PECHS Block-6, Shahrah-e-Faisal,
Karachi – 75350

First Floor, Blue Mall 8-R, MM Alam Road Gulberg III, Lahore

Tel: +92-21-3432 3721-4 

© 2020, Folio3 Software Inc., All rights reserved.

  • Privacy policy and terms of use
  • Cookie Policy
Follow us on
Facebook-f
Twitter
Linkedin-in

Get a free app audit

Tired of your app not performing up to the mark?

Get a free technology and app strategy review.