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
  • 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: Noc Folio3 | November 9, 2020
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:


About Noc Folio3

Newsletter

Search

Archives

  • December 2023
  • April 2023
  • March 2023
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • October 2021
  • September 2021
  • May 2021
  • February 2021
  • January 2021
  • 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

Recent Posts

  • Exploring Flutter Navigation: From Basics to Advanced Routes
  • Web UI Test Automation with Pytest-BDD
  • How to fix IOS compass calibration issues
  • Testing Android Applications With Perfect Coverage
  • How to use useRef hook efficiently? – React

Tags

  • android
  • angular-state-management
  • Automation
  • Compass
  • cross-platform
  • css
  • development
  • firebase
  • hooks
  • ios
  • learn-ngrx
  • ngrx-beginner
  • ngrx/store
  • QA
  • react-native
  • reactjs
  • scss
  • stylesheet
  • styling
  • Testing
  • Test Script
  • UI-UX

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
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

Folio3 Tower, Plot 26, Block B, SMCH Society, Main Shahrah-e-Faisal, Karachi.

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

Tel: +92-21-3432 3721-4 

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

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

Get a free app audit

[contact-form-7 id="3548" title="Float Banner Form"]