← Back to Chapters

List Rendering using map()

? List Rendering using map()

? Quick Overview

In React Native, lists are commonly rendered using the JavaScript map() function. It allows you to loop through an array and return UI components dynamically.

? Key Concepts

  • map() converts array items into components
  • Each item must have a unique key
  • Commonly used with View, Text, and FlatList

? Syntax / Theory

The map() function iterates over an array and returns JSX for each element. This is the most basic way to display a list in React Native.

? Code Example(s)

? View Code Example
// Rendering a list using map() in React Native
import React from 'react';
import { View, Text } from 'react-native';

export default function App() {
  const fruits = ['Apple', 'Banana', 'Mango', 'Orange'];

  return (
    <View>
      {fruits.map((item, index) => (
        <Text key={index}>{item}</Text>
      ))}
    </View>
  );
}

? Live Output / Explanation

Rendered Output

  • Apple
  • Banana
  • Mango
  • Orange

Each array element is converted into a Text component using map().

? Interactive Example

Below is a simulation of how map() works dynamically. Add or remove items to see how the list (UI) updates instantly based on the data array.

Rendered List (UI):
 
Underlying Data Array:

? Use Cases

  • Displaying user lists
  • Product catalogs
  • Menu items
  • Simple static lists

✅ Tips & Best Practices

  • Always provide a unique key
  • Use FlatList for large datasets
  • Keep list components lightweight

? Try It Yourself

  • Add styles to each list item
  • Render objects instead of strings
  • Replace index with unique IDs