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
style={styles.emptyListStyle}
onPress={() => getItem(item)}>
No Data Found
</Text>
const ItemView = ({item}) => {
return (
// Flat List Item
style={styles.itemStyle}
onPress={() => getItem(item)}>
{item.id}
{'.'}
{item.title.toUpperCase()}
</Text>
const ItemSeparatorView = () => {
return (
// Flat List Item Separator
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