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={this.state.dataSource}
  //dataSource to add data in the list
  ListHeaderComponent={this.renderHeader}
  //Header to show above listview
  ListFooterComponent={this.renderFooter}
  //Footer to show below listview
  ItemSeparatorComponent={this.ListViewItemSeparator}
  //List Item separator
  renderItem={({ item }) => (
    //Rendering Single Row
    <Text
      style={styles.rowViewContainer}
      onPress={this.showItem.bind(this, item)}>
      {item}
    </Text>
  )}
  keyExtractor={(item, index) => index.toString()}
/>

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

//This is an example to Add Header Footer in FlatList
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text, Alert, FlatList } from 'react-native';
//import all the components we are going to use.

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      //Making data set for list
      dataSource: [
        'Button',
        'Card',
        'Input',
        'Avatar',
        'CheckBox',
        'Header',
        'Icon',
        'Lists',
        'Rating ',
        'Pricing',
        'Avatar',
        'CheckBox',
        'Header',
        'Icon',
        'Lists',
        'Rating ',
        'Pricing',
      ],
    };
  }
  ListViewItemSeparator = () => {
    return (
      //List Item separator View
      <View
        style={{ height: 0.5, width: '100%', backgroundColor: '#606070' }}
      />
    );
  };
  showItem(rowData) {
    //Alert on the click of list Item
    Alert.alert(rowData);
  }
  renderHeader = () => {
    //View to set in Header
    return (
      <View style={styles.header_footer_style}>
        <Text style={styles.textStyle}> This is Header </Text>
      </View>
    );
  };
  renderFooter = () => {
    //View to set in Footer
    return (
      <View style={styles.header_footer_style}>
        <Text style={styles.textStyle}> This is Footer </Text>
      </View>
    );
  };
  render() {
    return (
      <View style={styles.MainContainer}>
        <FlatList
          data={this.state.dataSource}
          //dataSource to add data in the list
          ListHeaderComponent={this.renderHeader}
          //Header to show above listview
          ListFooterComponent={this.renderFooter}
          //Footer to show below listview
          ItemSeparatorComponent={this.ListViewItemSeparator}
          //List Item separator
          renderItem={({ item }) => (
            //Rendering Single Row
            <Text
              style={styles.rowViewContainer}
              onPress={this.showItem.bind(this, item)}>
              {item}
            </Text>
          )}
          keyExtractor={(item, index) => index.toString()}
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: {
    justifyContent: 'center',
    flex: 1,
    paddingTop: 20,
  },
  rowViewContainer: {
    padding: 10,
    fontSize: 18,
    height: 44,
  },
  header_footer_style: {
    width: '100%',
    height: 45,
    backgroundColor: '#606070',
  },
  textStyle: {
    textAlign: 'center',
    color: '#fff',
    fontSize: 18,
    padding: 7,
  },
});

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
react-native run-ios (macOS only).

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


Related Posts

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.