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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
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
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
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. 🙂
Good tutorial.