How to divide list items in sections with headings in React Native?

Table of Contents Hide
  1. Code Example
    1. Live Demo
  2. Conclusion

We often get the requirements to divide a long list into separate sections. These sections could be categories of items like colors, vegetables, fruits, drinks, football players, musicians, marvel superheroes etc.

In app development we use this kind of list for displaying menu. Where menu items are divided into categories and sub-categories.

React Native provides SectionList for this purpose. It has a prop renderSectionHeader which creates header for each section.

Code Example

import React, { useState } from "react";
import { SectionList, SafeAreaView, StyleSheet, Text, View, StatusBar } from "react-native";

const SUPERHERO_DATA = [
  {
    title: 'marvel',
    data: ['Ironman', 'Thor', 'Captain America', 'Hulk'],
  },
  {
    title: 'dc',
    data: ['Superman', 'Batman', 'Wonder Woman', 'Flash'],
  },
];

const App = () => {
  
  const renderItem = ({ item }) => (
    <View style={styles.item}>
        <Text>{item}</Text>
      </View>
  )

  return (
    <SafeAreaView style={styles.container}>
      <SectionList
        sections={SUPERHERO_DATA}
        keyExtractor={(item, index) => item + index}
        renderItem={renderItem}
        renderSectionHeader={({ section: { title } }) => (
          <Text style={styles.header}>{title}</Text>
        )}
      />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: StatusBar.currentHeight || 0,
  },
  header: {
    backgroundColor: '#888000',
    padding: 10,
    color: '#FFFFFF',
  },
  item: {
    backgroundColor: '#fff000',
    padding: 10,
    flex:1,
    margin: 5,
  },
});

export default App;

The output will look like this –

creating a sectionlist in React Native

In the above image you can see that the list is divided into 2 sections – Marvel and DC.

Let’s understand the code –

First, we declared an array of objects (SUPERHERO_DATA) with keys – title and data on line 4. Here title signifies name of section header. data is again an array of list items belonging to the section.

Each value of data array of all the objects are passed to function in renderItem prop at line 28. We can use this value and create a UI for list item.

We pass the SUPERHERO_DATA array to SectionList through sections prop at line 26.

Lastly, the section header or title is rendered through renderSectionHeader prop at line 29. It accepts the complete data object (with header title and items). Then we can pick the appropriate header value like we picked title from object and create a UI.

This way the whole section list can be generated.

You can do all the operations in SectionList the you could do in FlatList.

Live Demo

Open Live Demo

Conclusion

To divide list items among their categories in React Native, we can use SectionList. It is similar to FlatList but can create sections. You can create custom headers and enjoy all the functionality in SectionList which FlatList offers.


React Native Series

Alert

  1. Basic Alert
  2. Dismiss on Clicking Outside
  3. Input Fields in Alert Dialog – Prompt()
  4. Dark-Light Theme of Dialog

ActivityIndicator

  1. Basic Circular Loader
  2. Change size of Circular Loader
  3. Show/hide Circular Loader
  4. Change color of Circular Loader

Button

  1. Simple Button
  2. Change Button Color
  3. Disable Button Click
  4. Disable touch sound on Button click

FlatList

  1. Simple List
  2. Single Item Selection from List
  3. Multiple Item Selection from List
  4. Adding separator between list items
  5. Multiple columns List
  6. Showing Message in Empty List
  7. Add Footer to the List
  8. Add Header to the List
  9. Horizontal List
  10. Inverted List
  11. Pull to Refresh in List
  12. Infinite Loading List

SectionList

  1. Section List

ScrollView

  1. ScrollView
  2. Stick Single Item at Header
  3. Stick Multiple Items at Header
  4. Stick Item at Footer
  5. Hide Sticky Element on Scroll

Image

  1. Display Image from remote url
  2. Display local storage image
  3. Display Base64 Image
  4. Display Gif & Webp Images
  5. Adding Blur to Image
  6. Displaying loader for Image
  7. Resizemode for Images
  8. Setting Default Placeholder Image
  9. Background Image

Modal

  1. Basic Modal
  2. Slide from bottom Modal
  3. Fade In Modal
  4. Transparent Overlay Modal

RefreshControl

  1. RefreshControl
  2. Change Refresh Loader Color
  3. Change Refresh Loader Size
  4. Change Refresh Loader Background Color
  5. Title under Refresh Loader
  6. Change color of title under refresh loader

StatusBar

  1. Get StatusBar Size
  2. Change StatusBar Background Color
  3. Display StatusBar icons & text in While Color
  4. Display StatusBar icons & text in Dark Color
  5. Hiding StatusBar
  6. Translucent StatusBar

Switch

  1. Simple Switch
  2. Disable Switch
  3. Change Switch Colors

Text

  1. Adding Text
  2. Bold Text
  3. Italic Text
  4. Underline Text
  5. Selecting Text for copy-paste
  6. Changing Highlight Color of Text Selection
  7. Fit text in View box
  8. Clickable anchors in text
  9. Truncate Lengthy Text

TextInput

  1. Simple Input Field
  2. Auto Capitalize Text in Input Field
  3. Multiline Input Field
  4. Hide Cursor in Input Field
  5. Clear input Field using X
  6. Clear input Field when focused
  7. Change Cursor Color in Input Field
  8. Disable input field
  9. Icon at the left of Input Field
  10. Dark-Light Keyboard
  11. Avoid Overlapping of Keyboard
  12. Limiting Characters in Input Field
  13. Numeric Keyboard
  14. Email Id Keyboard
  15. Phone number Keyboard
  16. Url Keyboard
  17. Placeholder in input field
  18. Placeholder Color in Input Field
  19. Password Input Field
  20. Programmatically select text in Input Field
  21. Change Text Selection Color in Input Field
  22. Select Whole text in Input Field on Focus
  23. Write text from center in input field
  24. Changing underline color of input field

TouchableWithoutFeedback

  1. TouchableWithoutFeedback

TouchableHighlight

  1. TouchableHighlight

TouchableOpacity

  1. TouchableOpacity

Pressable

  1. Pressable
  2. Creating Ripple Effect

Appearance

  1. Dark-Light System Color Scheme

AppState

  1. AppState – Foreground/Background State of App

ToastAndroid

  1. Creating Android Toast Message

Dimensions

  1. Getting Screen & Window Dimensions

Keyboard

  1. Dismiss Keyboard Programmatically

👉 Learn Material Design using React Native Paper