Lists in React
Explore how to render dynamic lists in React by using the JavaScript map method within JSX. Understand the importance of stable keys for list items to help React identify changes efficiently. Learn to display multiple properties of list items and apply dynamic HTML attributes in a React component.
We'll cover the following...
We’ll experiment with sample data at first, then we’ll apply that to fetch data from a remote API. First, let’s define the array as a variable. As before, we can define a variable outside or inside the component. The following defines it outside:
I used a ... here as a placeholder, to keep my code snippet small (without App component implementation details) and focused on the essential parts (the list variable outside of the App component). I will use the ... throughout the rest of this learning experience as a placeholder for code blocks that I have established in previous exercises. If you get lost, you can always verify your code by running it live on the Educative SPA widget below.
Each item in the list has a title, a url, an author, an identifier (objectID), points – which indicate the popularity of an item – and a count of comments. Next, we’ll render the list within our JSX dynamically:
To display a list of items, we rely on standard JavaScript functionality. This “map in react” pattern is the most common way to transform an array of data into visual elements.
You can use the built-in JavaScript map method for arrays to iterate over each item of the list and return a new version of each:
We won’t map from one JavaScript data type to another in our case. Instead, we return a JSX fragment that renders each item of the list:
.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}
.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}
.App-title {
font-size: 1.5em;
}
.App-intro {
font-size: large;
}
@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
Actually, one of my first React “Aha” moments was using barebones JavaScript to map a list of JavaScript objects to HTML elements without any other HTML templating syntax. It’s just JavaScript in HTML.
React will display each item now, but you can still improve your code so React handles advanced dynamic lists more gracefully. By assigning a key attribute to each list item’s element, React can identify modified items if the list changes (e.g. re-ordering). Fortunately, our list items come with an identifier:
.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}
.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}
.App-title {
font-size: 1.5em;
}
.App-intro {
font-size: large;
}
@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
We avoid using the index of the item in the array to make sure the key attribute is a stable identifier. If the list changes its order, for example, React will not be able to identify the items properly:
So far, only the title is displayed for each item. Let’s experiment with displaying more of the item’s properties:
.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}
.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}
.App-title {
font-size: 1.5em;
}
.App-intro {
font-size: large;
}
@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
The map function is inlined concisely in your JSX. Within the map function, we have access to each item and its properties. The url property of each item is used as dynamic href attribute for the anchor tag. Not only can JavaScript in JSX be used to display items, but also to assign HTML attributes dynamically.
Let’s see the rendered list’s elements in the browser.