Open In App

What is the use of FlatList ?

In this article, we are going to learn about the uses of the FlatList Component. So firstly let’s know what is FlatListt?The FlatList component is an efficient way to display items in a scrollable list view. This component has many supported features such as Scroll Loading, header/footer views support, horizontal mode, pull to refresh, etc.

Syntax:



<FlatList
  data={}
  renderItem={}
/> 

In the above syntax, we can see that the FlatList component has two required props i.e. data and renderItem.

These two props of the FlatList component are compulsory to use. There are many other props in FlatList components that can also be used.



Props in FlatList:

Uses of FlatList:

Now let’s start with the implementation:

Step 1: Open your terminal and install expo-cli by the following command.

npm install -g expo-cli

Step 2: Now create a project by the following command.

expo init myapp

Step 3: Now go into your project folder i.e. myapp

cd myapp

Project Structure:

gfg

Example 1:  In this example, we have created a simple flat list.




import { FlatList, StyleSheet, Text, View } from "react-native";
  
const DATA = [
    {
      id:"1",
      title:"Data Structures"
    },
    {
      id:"2",
      title:"STL"
    },
    {
      id:"3",
      title:"C++"
    },
    {
      id:"4",
      title:"Java"
    },
    {
      id:"5",
      title:"Python"
    },
    {
      id:"6",
      title:"CP"
    },
    {
      id:"7",
      title:"ReactJs"
    },
    {
      id:"8",
      title:"NodeJs"
    },
    {
      id:"9",
      title:"MongoDb"
    },
    {
      id:"10",
      title:"ExpressJs"
    },
    {
      id:"11",
      title:"PHP"
    },
    {
      id:"12",
      title:"MySql"
    },
];
  
export default function App() {  
return (
  <View style={styles.container}>
    <FlatList
      data={DATA}
      renderItem={({ item }) => <Text style=
         {styles.item}>{item.title}</Text>}
      keyExtractor={(item) => item.id}/>
  </View>
  );
 }
   
const styles = StyleSheet.create({
  container: {
    flex:1,
      marginTop:30,
      padding:2,
    },
  item: {
    backgroundColor: 'green',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
        fontSize: 25,
  },
});

Step to run the application: Start the server by using the following command.

npm run android

Output:

gfg

Example 2: In this example, we have created the FlatList with a separator between each data item.




import React from "react";
import { FlatList, StyleSheet, Text, View } from "react-native";
  
const DATA = [
    {
      id:"1",
      title:"Data Structures"
    },
    {
      id:"2",
      title:"STL"
    },
    {
      id:"3",
      title:"C++"
    },
    {
      id:"4",
      title:"Java"
    },
    {
      id:"5",
      title:"Python"
    },
    {
      id:"6",
      title:"CP"
    },
    {
      id:"7",
      title:"ReactJs"
    },
    {
      id:"8",
      title:"NodeJs"
    },
    {
      id:"9",
      title:"MongoDb"
    },
    {
      id:"10",
      title:"ExpressJs"
    },
    {
      id:"11",
      title:"PHP"
    },
    {
      id:"12",
      title:"MySql"
    },
];
  
export default function App() {
  
  const Separator = () => {
    return <View style={{ 
        height: 4, 
        backgroundColor: "red"
        marginHorizontal: 3}} 
    />;
    };
    
return (
  <View style={styles.container}>
    <FlatList
      data={DATA}
      renderItem={({ item }) => <Text style=
        {styles.item}>{item.title}</Text>}
      keyExtractor={(item) => item.id}
      ItemSeparatorComponent={Separator}
    />
  </View>
  );
 }
   
const styles = StyleSheet.create({
  container: {
    flex:1,
      marginTop:30,
      padding:2,
    },
  item: {
    backgroundColor: 'green',
      padding: 20,
       marginHorizontal: 16,
       marginVertical:8,
        fontSize: 25,
  },
});

Explanation: In this example, we have created an array of data then we make a function called “Separator”. After that, we assign that function to ItemSeparatorComponent prop of FlatList Component which is used to add a separator between each item.

Output:

Reference: https://reactnative.dev/docs/flatlist


Article Tags :