How to Create Lists in React

React

readTime

7 min

How to Create Lists in React

React is a powerful library for building user interfaces, and one of the most common tasks you'll encounter is rendering lists. Whether it's a list of to-do items, products in a shopping cart, or menu options, lists are essential to nearly every application. In React, lists are typically rendered using the map() method, which is part of JavaScript's array prototype. In this guide, we'll break down how to create and efficiently manage lists in React.

Why Do We Need Lists in React?

Lists are everywhere in web development. Think about all the places where you're showing a collection of similar items, like:

  • 📝 A to-do list
  • 🛒 Products in an e-commerce store
  • 📋 A list of posts or comments in a blog

In React, lists are key to dynamically displaying data. Instead of manually writing HTML for each item (which can become overwhelming), React's ability to render arrays of elements allows us to dynamically create lists with ease.

How Do Lists Work in React?

In React, you can easily create a list of elements by mapping over an array and returning JSX for each item. The map() method is the go-to way for iterating over arrays in React. Here’s a quick refresher on map():

javascript
const numberArray = [1, 2, 3, 4, 5];
const updatedNumsExample = numberArray.map((number) => {
  return number + 2;
});
console.log(updatedNumsExample);

// Output: [3, 4, 5, 6, 7]

This method takes an array and applies a function to each item, returning a new array. In React, this works great because we can map over an array of data and return JSX (React's syntax for rendering HTML) for each item in the array.

Example of Rendering a List in React

Let’s start with a basic example of how you can render a list in React using the map() method.

javascript
const numbers = [1, 2, 3, 4, 5];

function NumberList() {
  return (
    <ul>
      {numbers.map((number) => (
        <li key={number.toString()}>{number}</li>
      ))}
    </ul>
  );
}

In this example, we:

  1. Have an array of numbers.
  2. Use map() to iterate over the array and return a <li> element for each number.
  3. Each <li> needs a unique key, which we'll discuss in the next section.

This renders a list of numbers as list items (<li>), and it’s done dynamically. No need to hard-code each list item manually.

Keys in React Lists: Why They Matter 🔑

When rendering lists in React, you may have noticed that each element inside a map() function requires a key prop. But why?

  • Performance: React uses keys to identify which items have changed, been added, or removed. This allows React to update only the necessary parts of the DOM, improving performance. 🚀
  • Uniqueness: The key prop should be unique among siblings in the list to ensure React can track elements efficiently.

Key Example

javascript
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) => (
  <li key={number.toString()}>{number}</li>
));

In the example above, we use the number itself as the key. This works because each number is unique. However, in real-world scenarios, you'll often be working with objects, and each object might have an id field. Use that for the key!

javascript
const todoItems = todos.map((todo) => <li key={todo.id}>{todo.text}</li>);

🔥 Best Practice: Use a Unique ID for Keys

Always use something unique for the key prop, like an id from your data. If your data doesn’t have unique IDs, you might be tempted to use the array index:

javascript
const todoItems = todos.map((todo, index) => <li key={index}>{todo.text}</li>);

While this works, it’s generally a bad practice. If the order of items in the list changes, React might get confused and make unnecessary DOM updates. It's better to have a unique identifier.

Rendering a List of Components in React

Rendering a simple list of elements is great, but in React, we often need to render custom components for each item in a list. This is where the power of React shines because components allow for more complex and reusable code.

Example: Rendering a List of Custom Components

Let’s say we have a list of people, and we want to render a custom Person component for each person in the list.

javascript
const people = [
  { id: "1", name: "Alice", age: 25 },
  { id: "2", name: "Bob", age: 30 },
  { id: "3", name: "Charlie", age: 35 },
];

function Person({ person }) {
  return (
    <li>
      {person.name} is {person.age} years old.
    </li>
  );
}

function PeopleList() {
  return (
    <ul>
      {people.map((person) => (
        <Person key={person.id} person={person} />
      ))}
    </ul>
  );
}

Here’s what’s happening:

  1. We have an array of objects, each representing a person.
  2. We create a Person component that accepts a person prop and renders the person's name and age.
  3. In the PeopleList component, we use map() to iterate over the people array and render a Person component for each person in the list, passing in the relevant data as props.

Each Person component is wrapped in an <li> because we're rendering a list, but the key takeaway here is how easy it is to reuse the Person component with different data.

Handling Empty Lists and Conditional Rendering 🤔

What happens if the list is empty? You don’t want to render an empty <ul>. React gives us a simple way to handle these cases with conditional rendering.

javascript
function PeopleList({ people }) {
  if (people.length === 0) {
    return <p>No people found.</p>;
  }

  return (
    <ul>
      {people.map((person) => (
        <Person key={person.id} person={person} />
      ))}
    </ul>
  );
}

Now, if the people array is empty, we’ll display a friendly message like, "No people found." Otherwise, the list of people will be displayed as usual. This kind of conditional rendering is essential when dealing with dynamic data that may or may not be available.

Using map() with Complex Data 💪

Sometimes, the data you’re working with isn’t as simple as a list of numbers or strings. You might be working with complex nested objects, and that’s okay! You can still use map() to navigate and render this data.

Let’s say we have a list of books, each with an author and a list of reviews. We want to render not just the book, but also the reviews for each book.

javascript
const books = [
  {
    id: "1",
    title: "The Great Gatsby",
    author: "F. Scott Fitzgerald",
    reviews: ["Amazing read!", "Loved the characters."],
  },
  {
    id: "2",
    title: "1984",
    author: "George Orwell",
    reviews: ["Chilling and thought-provoking."],
  },
];

function Book({ book }) {
  return (
    <li>
      <h2>{book.title}</h2>
      <p>By: {book.author}</p>
      <ul>
        {book.reviews.map((review, index) => (
          <li key={index}>{review}</li>
        ))}
      </ul>
    </li>
  );
}

function BookList() {
  return (
    <ul>
      {books.map((book) => (
        <Book key={book.id} book={book} />
      ))}
    </ul>
  );
}

Here, we:

  1. Have a list of books, each with multiple reviews.
  2. Create a Book component that renders the title, author, and reviews for each book.
  3. For the reviews, we use map() within the Book component to render each review inside a nested <ul>.

This is an excellent example of how flexible map() can be when rendering nested structures.

Wrapping Up 🎉

Creating lists in React is a crucial part of building interactive UIs, and understanding how to effectively use the map() method, along with keys and conditional rendering, will help you build more dynamic and responsive applications.

  • 📝 Always use unique keys for list items to optimize performance.
  • 🔄 Use map() to iterate over arrays and render components dynamically.
  • ⚡ Handle empty states and conditionally render components for better user experience.

With this knowledge, you're ready to tackle all kinds of lists in your React applications! Happy coding! 😎

authorImg

Witek Pruchnicki

I passionately share knowledge about programming and more in various ways.