DEV Community

Cover image for Exploring Lists in React Native: Choosing the Right Component
Shevon Soyza
Shevon Soyza

Posted on

Exploring Lists in React Native: Choosing the Right Component

Introduction

When it comes to developing user-friendly, optimised apps with the React Native framework, lists play an important role. Lists help us organise and present the information neatly in the apps. React Native offers different types of list components for different scenarios. In this article, we will discuss:

  • FlatList
  • SectionList
  • ScrollView
  • VirtualizedList
  • GridList

Among them, choosing the right list component will help increase the app’s performance and user experience. So, let’s take a quick look at these components.

FlatList: Efficient Rendering for Long Lists

When dealing with a lengthy dataset where we need to show lots of things in order, the FlatList component is the go-to solution. Also, it has a virtualization technique that only shows what you’re looking at right now. Unlike traditional lists that render all items at once, FlatList renders only the items currently visible on the screen. This means our app will load faster and use less memory.

Here's a simple example:

import React from 'react';
import {FlatList, View, Text} from 'react-native';

const data = [
  {id: '1', title: 'Item 1'},
  {id: '2', title: 'Item 2'},
  {id: '3', title: 'Item 3'},
  // ...more data
];

const FlatListComponent = () => {
  return (
    <FlatList
      data={data}
      keyExtractor={item => item.id}
      renderItem={({item}) => (
        <View>
          <Text>{item.title}</Text>
        </View>
      )}
    />
  );
};

export default FlatListComponent;

Enter fullscreen mode Exit fullscreen mode

SectionList: Categorising Data with Style

The SectionList is a component that puts things into groups with headings. If the app involves organising data into sections, we can use this component. It enables you to create lists with different sections, each with its own header. For example, let’s say we are making a recipe application and need to group the recipes by type.

import React from 'react';
import {SectionList, View, Text} from 'react-native';

const recipes = [
  {
    title: 'Desserts',
    data: [{name: 'Cake', ingredients: 'Flour, Sugar'} /* ... */],
  },
  // ... more types of recipes
];

const SectionListComponent = () => {
  return (
    <SectionList
      sections={recipes}
      keyExtractor={item => item.name}
      renderSectionHeader={({section: {title}}) => (
        <Text style={{fontSize: 20, fontWeight: 'bold'}}>{title}</Text>
      )}
      renderItem={({item}) => (
        <View>
          <Text>{item.name}</Text>
          <Text>{item.ingredients}</Text>
        </View>
      )}
    />
  );
};

export default SectionListComponent;

Enter fullscreen mode Exit fullscreen mode

ScrollView: Flexibility in Complex Layouts

Now, let’s discuss ScrollView. It’s not exactly a list component, but it allows you to create scrollable layouts with different contents like text, images, and other components.
So, ScrollView is best for layout screens with mixed content or complex layouts. Here's a simple example:

import React from 'react';
import {ScrollView, Text, Image} from 'react-native';

const ScrollViewComponent = () => {
  return (
    <ScrollView>
      <Text>Some text content</Text>
      <Image source={require('./image.jpg')} />
      {/* ...more components */}
    </ScrollView>
  );
};

export default ScrollViewComponent;

Enter fullscreen mode Exit fullscreen mode

VirtualizedList: Unveiling the Magic of Efficient Rendering

This virtualizedList component will optimise memory usage and rendering performance of the app by recycling components. It ensures smooth scrolling and gives plenty of room for customization. Also, FlatList and SectionList are powered by the underlying VirtualizedList. And when we need more flexibility than what FlatList provides, we can simply use VirtualizedList. Here’s an example code:

import React from 'react';
import {VirtualizedList, View, Text} from 'react-native';

const data = [
  {id: '1', title: 'Item 1'},
  {id: '2', title: 'Item 2'},
  // ...more data
];

const VirtualizedListComponent = () => {
  return (
    <VirtualizedList
      data={data}
      getItemCount={data.length}
      getItem={(data, index) => data[index]}
      keyExtractor={item => item.id}
      renderItem={({item}) => (
        <View>
          <Text>{item.title}</Text>
        </View>
      )}
    />
  );
};

export default VirtualizedListComponent;

Enter fullscreen mode Exit fullscreen mode

GridList: Making Pretty Grids

There’s no built-in component called GridList in React Native. But still, we can make grid-like layouts using a combination of View and styling. Or we can achieve grid layouts using the versatile FlatList. Here's an example where we need to display content like image galleries or product listings in grid format.

import React from 'react';
import {View, Text} from 'react-native';

// Imagine you have product info
const products = [
  {id: 1, name: 'Cool Product 1', price: 19.99},
  // ... more products
];

const GridLayoutComponent = () => {
  // Here's how you make a grid
  return (
    <View>
      {products.map(product => (
        <View key={product.id}>
          <Text>{product.name}</Text>
          <Text>${product.price}</Text>
        </View>
      ))}
    </View>
  );
};

export default GridLayoutComponent;

Enter fullscreen mode Exit fullscreen mode
import React from 'react';
import {View, FlatList, Image} from 'react-native';

const data = [
  {id: '1', image: require('./image1.jpg')},
  {id: '2', image: require('./image2.jpg')},
  // ...more data
];

const GridLayoutComponent = () => {
  return (
    <FlatList
      data={data}
      keyExtractor={item => item.id}
      numColumns={2}
      renderItem={({item}) => (
        <View>
          <Image source={item.image} />
        </View>
      )}
    />
  );
};

export default GridLayoutComponent;

Enter fullscreen mode Exit fullscreen mode

Picking the Right List: Easy Tips

  • FlatLists for large datasets and to ensure smooth performance.
  • SectionList for when data needs to be categorised.
  • ScrollView for when the layout design involves mixed content.
  • VirtualizedList to create your own customised lists if none of the built-in components meet your needs.
  • Grid Layout for making grids using View and Stylings.

Every app is unique. By understanding the strengths and use cases for the above FlatList, SectionList, ScrollView, VirtualizedList and GridList; we can develop a highly engaging and efficient cross-platform app in React Native. Happy Coding!

Top comments (0)