Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to loop through an array in ReactJS

Understanding Arrays in ReactJS

Before we dive into the world of loops and arrays in ReactJS, let's first grasp what arrays are. Think of arrays as a long shelf in your garage, where you can store multiple items in one place. Each slot in the shelf represents an element in the array, and each has a unique address, the index. In JavaScript, arrays are objects that allow us to store multiple values in a single variable.

What is Looping?

In the world of programming, looping is like a tour of a museum. Imagine you are a tour guide, and you have to take a group of visitors through every exhibit. You would start at the first one, move to the next, and so on, until you've visited them all. This is essentially what looping does. It allows us to go through each element in an array, perform some computation, and move to the next one.

Looping Through An Array in ReactJS

In ReactJS, we often need to display lists or collections on the user interface. This is where looping comes in handy. To illustrate this, let's create an array of superheroes.

const superHeroes = ['Superman', 'Batman', 'Wonder Woman', 'Flash', 'Aquaman'];

Now, we want to display a list of these superheroes on the webpage. How do we do that? ReactJS has a built-in method .map() for arrays that we can use to loop through an array.

The Map Function

The map() function is like a factory production line. It takes each element (raw material), performs some operation (assembly), and outputs a new element (finished product).

superHeroes.map((hero) => {
  return <li>{hero}</li>;
});

In the above code, hero is a parameter that represents the current element being processed in the array. The map() function returns a new array that contains the results of applying a function to every element in the array.

Displaying the List in JSX

JSX, or JavaScript XML, is an extension for JavaScript that allows us to write HTML in React. It makes the syntax more readable and writeable. We can insert our map() function in curly braces {} inside our JSX code to display the array elements.

function App() {
  const superHeroes = ['Superman', 'Batman', 'Wonder Woman', 'Flash', 'Aquaman'];

  return (
    <div className="App">
      <h1>Super Heroes</h1>
      <ul>
        {superHeroes.map((hero) => {
          return <li>{hero}</li>;
        })}
      </ul>
    </div>
  );
}

export default App;

With the above code, we have a list of our superheroes displayed on the webpage. Isn't that super cool?

Key Prop in Lists

When we use the map() function in React to render a list, React needs a way to identify each rendered list item. This is where the 'key' prop comes in. It's like a unique ID card for each item in the array.

{superHeroes.map((hero, index) => {
  return <li key={index}>{hero}</li>;
})}

In the above code, we added a second parameter index to the map() function. This index is the unique address of each superhero in our array, which we used as a key.

Conclusion

Loops and arrays are some of the most potent tools in a developer's toolkit. They are the bread and butter of displaying dynamic data in ReactJS. The map() function is like our magic wand that transforms our raw array data into beautiful lists on our webpage.

Remember, the map() function is not just a tool for rendering lists in React. It's a powerful function that you can leverage to manipulate arrays and create new ones. It's like having a mini factory at your disposal. So, keep practicing, explore more use-cases, and soon, you'll be mapping arrays like a pro. Happy coding!