Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to go through an array in ReactJS

Introduction to Arrays in ReactJS

If you're just beginning your journey into the world of programming, you may find yourself grappling with various concepts and terminologies. One such concept is the array. In its simplest form, an array is a collection of items stored in sequential order.

Imagine you have a shelf where you want to store your collection of books. Each slot on this shelf represents an index in the array, and the book it holds represents the item. This is exactly how an array works in ReactJS!

Now, let's get our hands dirty and dive into the practical side of things.

Setting Up Our Environment

Before we start, we need to set up our coding environment. We'll be using CodePen for this tutorial, which is a fantastic online editor. If you're not familiar with CodePen, think of it as a digital playground where you can write and test your code.

const { useState } = React;

function App() {
  const [books, setBooks] = useState([
    "To Kill a Mockingbird",
    "1984",
    "Harry Potter",
    "The Great Gatsby",
    "The Catcher in the Rye"
  ]);

  return (
    <div className="App">
      <h1> My Bookshelf </h1>
      { /* We will fill this in later */ }
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));

In the above code, we've created a functional component called 'App'. We've also created a state variable 'books' using the useState hook, which is a feature in React that lets you add state to your functional components.

Iterating Over an Array Using the map() Function

Now that our environment is set up, let's look at how to go through our array. In ReactJS, one common way to iterate over an array is by using the map() function. The map() function is a higher-order function, which is just a fancy way of saying it's a function that operates on other functions.

In this case, map() takes a function as an argument and applies this function to every element in our array, much like how a postman delivers a letter to every house in a street.

Here's how we can use the map() function to display the list of books on our bookshelf:

function App() {
  const [books, setBooks] = useState([
    "To Kill a Mockingbird",
    "1984",
    "Harry Potter",
    "The Great Gatsby",
    "The Catcher in the Rye"
  ]);

  return (
    <div className="App">
      <h1> My Bookshelf </h1>
      {
        books.map((book) => {
          return <h2>{book}</h2>
        })
      }
    </div>
  );
}

In the above code, we use the map() function to go through each book in our books array. For each book, we return a <h2> element containing the book's name.

Keys in Lists

When creating lists in ReactJS, we need to include a unique key prop for each item. This key helps React identify which items have changed, are added, or are removed, which is important when the application renders the components.

Think of the key prop as a unique ID or a barcode on each book in your collection. This way, even if you move your books around or add new ones, you can always identify a book by its unique barcode.

Let's add a key to our list items:

function App() {
  const [books, setBooks] = useState([
    "To Kill a Mockingbird",
    "1984",
    "Harry Potter",
    "The Great Gatsby",
    "The Catcher in the Rye"
  ]);

  return (
    <div className="App">
      <h1> My Bookshelf </h1>
      {
        books.map((book, index) => {
          return <h2 key={index}>{book}</h2>
        })
      }
    </div>
  );
}

Here, we use the index of each book in our array as its key. This works in our case because our list of books won't be re-ordered, and no book will be added or removed.

Conclusion

There you have it; you now know how to go through an array in ReactJS! Remember, arrays are like bookshelves, and the map() function is like a diligent postman, delivering to each house (or in our case, each element in the array). Don't forget about the importance of keys. They're like unique barcodes that help React keep track of each item in the list.

Learning to code can sometimes feel overwhelming, but with practice and patience, you'll soon get the hang of it. Happy coding!