React Native Add Header Footer in ListView / FlatList

React Native ListView

Here is an example of React Native Add Header Footer in ListView / FlatList. As the topic name describes we will add the header and footer in FlatList. To add header and footer in FlatList we will use ListHeaderComponent and ListFooterComponent props of the FlatList same as we use ItemSeparatorComponent while adding FlatList Item Separator.

Add Header Footer in FlatList

Here is the code which we have used to add header and footer in ListView

<FlatList
  data={dataSource}
  keyExtractor={(item, index) => index.toString()}
  ItemSeparatorComponent={ItemSeparatorView}
  //Header to show above listview
  ListHeaderComponent={ListHeader}
  //Footer to show below listview
  ListFooterComponent={ListFooter}
  renderItem={ItemView}
  ListEmptyComponent={EmptyListMessage}
/>

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Code to add header footer in FlatList

Open App.js in any code editor and replace the code with the following code

App.js

// React Native Add Header Footer in ListView / FlatList
// https://aboutreact.com/react-native-add-header-footer-in-listview/

// import React in our code
import React, {useState} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text,
  FlatList,
} from 'react-native';

const App = () => {
  const [dataSource, setDataSource] = useState([
    {id: 1, title: 'Button'},
    {id: 2, title: 'Card'},
    {id: 3, title: 'Input'},
    {id: 4, title: 'Avatar'},
    {id: 5, title: 'CheckBox'},
    {id: 6, title: 'Header'},
    {id: 7, title: 'Icon'},
    {id: 8, title: 'Lists'},
    {id: 9, title: 'Rating'},
    {id: 10, title: 'Pricing'},
    {id: 11, title: 'Avatar'},
    {id: 12, title: 'CheckBox'},
    {id: 13, title: 'Header'},
    {id: 14, title: 'Icon'},
    {id: 15, title: 'Lists'},
    {id: 16, title: 'Rating'},
    {id: 17, title: 'Pricing'},
  ]);

  const EmptyListMessage = ({item}) => {
    return (
      // Flat List Item
      <Text
        style={styles.emptyListStyle}
        onPress={() => getItem(item)}>
        No Data Found
      </Text>
    );
  };

  const ItemView = ({item}) => {
    return (
      // Flat List Item
      <Text
        style={styles.itemStyle}
        onPress={() => getItem(item)}>
        {item.id}
        {'.'}
        {item.title.toUpperCase()}
      </Text>
    );
  };

  const ItemSeparatorView = () => {
    return (
      // Flat List Item Separator
      <View
        style={{
          height: 0.5,
          width: '100%',
          backgroundColor: '#C8C8C8',
        }}
      />
    );
  };

  const ListHeader = () => {
    //View to set in Header
    return (
      <View style={styles.headerFooterStyle}>
        <Text style={styles.textStyle}>
            This is Header
        </Text>
      </View>
    );
  };

  const ListFooter = () => {
    //View to set in Footer
    return (
      <View style={styles.headerFooterStyle}>
        <Text style={styles.textStyle}>
            This is Footer
        </Text>
      </View>
    );
  };

  const getItem = (item) => {
    // Function for click on an item
    alert('Id : ' + item.id + ' Title : ' + item.title);
  };

  return (
    <SafeAreaView style={{flex: 1}}>
      <FlatList
        data={dataSource}
        keyExtractor={(item, index) => index.toString()}
        ItemSeparatorComponent={ItemSeparatorView}
        //Header to show above listview
        ListHeaderComponent={ListHeader}
        //Footer to show below listview
        ListFooterComponent={ListFooter}
        renderItem={ItemView}
        ListEmptyComponent={EmptyListMessage}
      />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  emptyListStyle: {
    padding: 10,
    fontSize: 18,
    textAlign: 'center',
  },
  itemStyle: {
    padding: 10,
  },
  headerFooterStyle: {
    width: '100%',
    height: 45,
    backgroundColor: '#606070',
  },
  textStyle: {
    textAlign: 'center',
    color: '#fff',
    fontSize: 18,
    padding: 7,
  },
});

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

Output in Online Emulator

This is how you can add header footer in ListView / FlatList in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

1 thought on “React Native Add Header Footer in ListView / FlatList”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.