Lesson 24 of 60 – Rendering Lists
40%

Rendering Lists

Rendering Lists in React means displaying multiple items from an array or another collection of data. React commonly uses JavaScript's map() method to create a list of JSX elements.

Note: When rendering a list in React, each item should have a unique key so React can efficiently identify individual elements.

1. What is a List?

A list is a collection of related items. For example, a list of students can be stored in a JavaScript array.

const students = [
    "Rahul",
    "Neha",
    "Amit",
    "Priya"
];

React can use this array to display multiple student names.

2. Why Render Lists in React?

List rendering is useful when an application has multiple records or repeated UI elements.

  • Display student lists.
  • Display course lists.
  • Display products.
  • Display menu items.
  • Display messages.
  • Display records received from an API.

3. Using the map() Method

The JavaScript map() method creates a new array by applying a function to every item in an existing array.

const numbers = [1, 2, 3];

const result = numbers.map(number => {
    return number * 2;
});

console.log(result);

The result will be:

[2, 4, 6]

4. Rendering a Simple List

The map() method can be used inside JSX to create list items.

function App() {

    const students = [
        "Rahul",
        "Neha",
        "Amit"
    ];

    return (
        <ul>

            {students.map(student => (
                <li key={student}>
                    {student}
                </li>
            ))}

        </ul>
    );

}

5. Rendering Numbers

Numbers can also be rendered from an array.

function App() {

    const numbers = [10, 20, 30, 40];

    return (
        <ul>

            {numbers.map(number => (
                <li key={number}>
                    {number}
                </li>
            ))}

        </ul>
    );

}

6. Rendering Courses

function Courses() {

    const courses = [
        "HTML",
        "CSS",
        "JavaScript",
        "React"
    ];

    return (
        <div>

            {courses.map(course => (
                <p key={course}>
                    {course}
                </p>
            ))}

        </div>
    );

}

7. Rendering List Items

A common pattern is to convert each array item into an li element.

const fruits = [
    "Apple",
    "Banana",
    "Mango"
];

function App() {

    return (
        <ul>

            {fruits.map(fruit => (
                <li key={fruit}>
                    {fruit}
                </li>
            ))}

        </ul>
    );

}

8. Understanding the key Prop

When rendering lists, React uses a special key prop to identify individual list items.

{students.map(student => (
    <li key={student}>
        {student}
    </li>
))}

The key should be unique among the items in the rendered list.

9. Why Keys are Important

Keys help React determine which items have changed, been added, or been removed.

  • Helps React identify list items.
  • Improves update handling.
  • Helps React preserve item identity.
  • Reduces unnecessary DOM updates.

10. Rendering Objects

Real applications usually work with arrays of objects rather than simple strings.

const students = [
    {
        id: 1,
        name: "Rahul",
        course: "React"
    },
    {
        id: 2,
        name: "Neha",
        course: "JavaScript"
    }
];

11. Rendering Object Properties

function StudentList() {

    const students = [
        {
            id: 1,
            name: "Rahul",
            course: "React"
        },
        {
            id: 2,
            name: "Neha",
            course: "JavaScript"
        }
    ];

    return (
        <div>

            {students.map(student => (
                <div key={student.id}>

                    <h2>
                        {student.name}
                    </h2>

                    <p>
                        Course: {student.course}
                    </p>

                </div>
            ))}

        </div>
    );

}

12. Using a Unique ID as Key

When objects contain unique IDs, the ID is usually a suitable choice for the React key.

{students.map(student => (
    <div key={student.id}>
        {student.name}
    </div>
))}

Using a stable unique identifier helps React correctly track items.

13. Rendering Cards

function Courses() {

    const courses = [
        {
            id: 1,
            name: "React",
            duration: "6 Months"
        },
        {
            id: 2,
            name: "Python",
            duration: "6 Months"
        }
    ];

    return (
        <div>

            {courses.map(course => (
                <div
                    className="card"
                    key={course.id}
                >

                    <h2>
                        {course.name}
                    </h2>

                    <p>
                        {course.duration}
                    </p>

                </div>
            ))}

        </div>
    );

}

14. Rendering Components from a List

The map() method can also be used to render reusable React components.

function StudentCard({ name, course }) {

    return (
        <div>

            <h2>
                {name}
            </h2>

            <p>
                {course}
            </p>

        </div>
    );

}
{students.map(student => (
    <StudentCard
        key={student.id}
        name={student.name}
        course={student.course}
    />
))}

15. Passing the Whole Object

The complete object can also be passed to a child component.

function StudentCard({ student }) {

    return (
        <div>

            <h2>
                {student.name}
            </h2>

            <p>
                {student.course}
            </p>

        </div>
    );

}
{students.map(student => (
    <StudentCard
        key={student.id}
        student={student}
    />
))}

16. Rendering Nested Data

Objects can contain arrays, and those arrays can also be rendered.

const student = {
    name: "Rahul",
    subjects: [
        "HTML",
        "CSS",
        "React"
    ]
};
{student.subjects.map(subject => (
    <li key={subject}>
        {subject}
    </li>
))}

17. Rendering a List with Index

The map() callback can receive both the item and its index.

const students = [
    "Rahul",
    "Neha",
    "Amit"
];

{students.map((student, index) => (
    <li key={student}>

        {index + 1}. {student}

    </li>
))}

The index can be useful for displaying numbering.

18. Using Index as Key

The array index can technically be used as a key:

{students.map((student, index) => (
    <li key={index}>
        {student}
    </li>
))}

However, when list items can be reordered, inserted, or removed, using a stable unique ID is generally preferable.

19. Filtering Before Rendering

The filter() method can be combined with map() to display only selected items.

const students = [
    {
        id: 1,
        name: "Rahul",
        active: true
    },
    {
        id: 2,
        name: "Neha",
        active: false
    },
    {
        id: 3,
        name: "Amit",
        active: true
    }
];

{students
    .filter(student => student.active)
    .map(student => (
        <p key={student.id}>
            {student.name}
        </p>
    ))
}

20. Sorting Before Rendering

Data can be sorted before it is rendered.

const students = [
    "Rahul",
    "Amit",
    "Neha"
];

const sortedStudents = [...students].sort();

{sortedStudents.map(student => (
    <li key={student}>
        {student}
    </li>
))}

Using a copied array avoids directly modifying the original array.

21. Conditional List Rendering

A list can be displayed conditionally when data is available.

function StudentList({ students }) {

    if (students.length === 0) {

        return (
            <p>
                No students found.
            </p>
        );

    }

    return (
        <ul>

            {students.map(student => (
                <li key={student.id}>
                    {student.name}
                </li>
            ))}

        </ul>
    );

}

22. Rendering a Menu

function Menu() {

    const items = [
        "Home",
        "About",
        "Courses",
        "Contact"
    ];

    return (
        <nav>

            {items.map(item => (
                <a
                    href="#"
                    key={item}
                >
                    {item}
                </a>
            ))}

        </nav>
    );

}

23. Rendering Products

function Products() {

    const products = [
        {
            id: 1,
            name: "Laptop",
            price: 50000
        },
        {
            id: 2,
            name: "Keyboard",
            price: 1500
        },
        {
            id: 3,
            name: "Mouse",
            price: 800
        }
    ];

    return (
        <div>

            {products.map(product => (
                <div key={product.id}>

                    <h2>
                        {product.name}
                    </h2>

                    <p>
                        ₹{product.price}
                    </p>

                </div>
            ))}

        </div>
    );

}

24. Rendering API Data

Lists are frequently created from data received from an API.

function Users({ users }) {

    return (
        <div>

            {users.map(user => (
                <div key={user.id}>

                    <h3>
                        {user.name}
                    </h3>

                    <p>
                        {user.email}
                    </p>

                </div>
            ))}

        </div>
    );

}

25. Common List Rendering Mistake

A common mistake is forgetting the key when rendering a list.

Incorrect:

{students.map(student => (
    <li>
        {student.name}
    </li>
))}

Better:

{students.map(student => (
    <li key={student.id}>
        {student.name}
    </li>
))}

26. Common Key Mistakes

  • Using the same key for every item.
  • Using a random value as a key.
  • Using an unstable value as a key.
  • Forgetting the key completely.
  • Using the array index when the list can change significantly.

A stable unique ID is generally a good choice when one is available.

27. List Rendering with Destructuring

Object properties can be destructured directly inside the map() callback.

const students = [
    {
        id: 1,
        name: "Rahul",
        course: "React"
    },
    {
        id: 2,
        name: "Neha",
        course: "Python"
    }
];

{students.map(({ id, name, course }) => (
    <div key={id}>

        <h2>
            {name}
        </h2>

        <p>
            {course}
        </p>

    </div>
))}

28. List Rendering with Components and Props

function CourseCard({ name, fee }) {

    return (
        <div className="card">

            <h2>
                {name}
            </h2>

            <p>
                Fee: ₹{fee}
            </p>

        </div>
    );

}

function CourseList() {

    const courses = [
        {
            id: 1,
            name: "React",
            fee: 10000
        },
        {
            id: 2,
            name: "Python",
            fee: 8000
        }
    ];

    return (
        <div>

            {courses.map(course => (
                <CourseCard
                    key={course.id}
                    name={course.name}
                    fee={course.fee}
                />
            ))}

        </div>
    );

}

29. Complete List Rendering Example

function StudentCard({
    name,
    course,
    city
}) {

    return (
        <div className="card">

            <h2>
                {name}
            </h2>

            <p>
                Course: {course}
            </p>

            <p>
                City: {city}
            </p>

        </div>
    );

}

function App() {

    const students = [
        {
            id: 1,
            name: "Rahul",
            course: "React",
            city: "Patna"
        },
        {
            id: 2,
            name: "Neha",
            course: "JavaScript",
            city: "Delhi"
        },
        {
            id: 3,
            name: "Amit",
            course: "Python",
            city: "Mumbai"
        }
    ];

    return (
        <div>

            <h1>
                Students
            </h1>

            {students.map(student => (
                <StudentCard
                    key={student.id}
                    name={student.name}
                    course={student.course}
                    city={student.city}
                />
            ))}

        </div>
    );

}

export default App;

This example uses an array of objects, map(), unique keys, props, and a reusable component to render student data.

30. List Rendering Best Practices

  • Use map() to transform arrays into JSX.
  • Give every rendered list item a suitable unique key.
  • Prefer stable unique IDs when available.
  • Avoid unnecessary use of array indexes as keys.
  • Use reusable components for complex list items.
  • Keep list rendering code readable.
  • Use filter() when only selected items should be displayed.
  • Handle empty lists appropriately.
  • Keep data processing separate when the rendering logic becomes complex.
  • Use props to pass list item data into reusable components.

📌 Key Points

  • React commonly uses JavaScript map() to render lists.
  • Arrays can contain strings, numbers, objects, or other data.
  • Objects are commonly rendered by accessing their properties.
  • Each list item should have a suitable unique key.
  • Stable IDs are generally preferred as keys.
  • Array indexes should be used carefully as keys.
  • Lists can render reusable React components.
  • filter() and map() can be combined for selective rendering.
  • List rendering is frequently used with API data.
  • Reusable components and props make complex lists easier to manage.

🧠 Quick Quiz

Question: Which JavaScript method is commonly used in React to render items from an array?