Adding item separators and header to FlatList

Adding item separators and header to FlatList

In this post we will give you information about Adding item separators and header to FlatList. Hear we will give you detail about Adding item separators and header to FlatListAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we’ll see how we can customize our FlatList component in our React Native app using a header and item separators.

The FlatList component allows you to add and customize the list header and item separators using the ListHeaderComponent and ItemSeparatorComponent props.

Let’s start with the item separator component. In the App.js file, add the following code:

FlatListItemSeparator = () => {  return (    <View      style={{        height: 1,        width: "100%",        backgroundColor: "#000",      }}    />  );}

We simply use a View component for our separator with a height of 1, a 100% width and a black background color. This will render an horizontal black line that takes the full width of the list.

Next, let’s add a header component in the same App.js file:

FlatListHeader = () => {  return (    <View elevation={1}       style={{        height: 100,        width: "97%",        margin: 5,        backgroundColor: "#fff",        border: 2.9,        borderColor: "black",        alignSelf: "center",        shadowColor: "#000",        shadowOffset: {          width: 0,          height: 16,        },        shadowOpacity: 1,        shadowRadius: 7.49      }}    >      <Text style={{  textShadowColor: 'black', textShadowOffset: { width: 1, height: 3 },textShadowRadius: 10, fontSize: 40, fontWeight: '800', flex: 1, alignSelf: "center", paddingTop: 30, fontSize: 40}}>Latest articles</Text>    </View>  );}

For the header, we used a View with a Text components. We have used the style prop for each component to add a bunch of inline styles, particularly the shadow properties for adding some shadows to the View container and Text component.

You can make use of this online React Native shadow generator for generating the shadow styles.

Now, let’s change our FlatList component by adding the ListHeaderComponent and ItemSeparatorComponent props and assigning the previous components:

const HomeScreen = (props) => {  console.log("articles: ", props.articles);  return (    <View>        <FlatList          data={ props.articles }          ListHeaderComponent = { this.FlatListHeader }             ItemSeparatorComponent = { this.FlatListItemSeparator }          keyExtractor={(item, index) => index.toString()}  renderItem={({item}) => <ArticleItem article = { item }  />}        />    </View>  );}

This is a screenshot of our UI with a list header, item separators and shadow around the header:

FlatList Header and Separators Example

We have a much better UI even with this little customization.

In the next part, we’ll implement the functionality of the open and save buttons attached to each FlastList item. We’ll be able to open the URL of each article in a web browser or save the article using local storage for later reading.


Hope this code and post will helped you for implement Adding item separators and header to FlatList. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US