Rendering Lists of Data
Rendering a list in React means turning an array of data, such as products, messages or search results, into an array of JSX elements, usually with the array's map() method. You use it whenever the number of items on screen depends on data rather than being written out by hand.
From array to elements with map
JSX can display an array of elements directly. map() takes each item, returns a piece of JSX for it, and gives you a new array to place inside your markup.
const expenses = [
{ id: 'e1', title: 'Rent', amount: 15000 },
{ id: 'e2', title: 'Groceries', amount: 4200 },
{ id: 'e3', title: 'Internet', amount: 799 },
];
function ExpenseList({ items, minAmount = 0 }) {
const visible = items.filter((e) => e.amount >= minAmount);
if (visible.length === 0) {
return <p>No expenses found.</p>;
}
return (
<ul>
{visible.map((expense) => (
<li key={expense.id}>
{expense.title}: {expense.amount}
</li>
))}
</ul>
);
}
// <ExpenseList items={expenses} minAmount={1000} />
Notice the parentheses after the arrow. They return the JSX implicitly. If you use curly braces instead, you must write return yourself, otherwise each item returns undefined and nothing appears.
Why every item needs a key
React uses the key prop to match items between renders. When the list changes, keys tell React which item was added, removed or moved, so it can update only those elements and keep the state of the others, such as text typed into an input inside a list item.
A good key is:
- Unique among siblings, though it can repeat in a different list.
- Stable, meaning the same item gets the same key on every render.
- Usually an id from your data, such as a database id.
Keys go on the outermost element returned from map. If each item is a component, put the key on the component: <ExpenseItem key={e.id} {...e} />, not on an element inside it.
The index-as-key trap
Using the array index, key={index}, makes the warning disappear, but it causes real bugs when the list can be reordered, filtered or have items inserted at the top. The keys then move to different items. React reuses the wrong elements, so an input typed in the first row can suddenly appear in the second, or an animation runs on the wrong item.
Index keys are acceptable only when the list never changes order and items are never inserted or removed in the middle. Generating a random key with Math.random() during render is worse: every render creates new keys, so React rebuilds every item from scratch.
Filtering, sorting and empty states
Prepare the data before mapping. Filter and sort into a new variable, as the example does with visible. When sorting, copy first with [...items].sort(...) or use toSorted(), because sort() changes the original array, which may be state or props you are not allowed to mutate.
Always plan for zero items. An empty <ul> looks like a broken page; a short message such as "No expenses found" tells users the app worked and simply had nothing to show.
Keeping list code tidy
- Move each row into its own component once it grows beyond a few lines.
- Keep
mapcallbacks free of side effects like fetching data or setting state. - For very long lists, consider pagination or "load more" rather than rendering everything at once.
Key point: map data to JSX, give each item a stable key from your data, and handle the empty case. Making the list stateful, so items can be added and removed, is the next step.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.