Introduction
Most interfaces show lists: messages, products, search results, rows of a table. In React you never write the items one by one; you keep the data in an array and turn it into elements with the array method map. This lesson shows how, and explains the one extra thing React needs from you: a key for each item.
How it works
Inside braces, JSX can show an array of elements, and map turns an array of data into exactly that:
<ul>
{fruits.map((f) => (
<li key={f.id}>{f.name}</li>
))}
</ul>
To show only some items, or in another order, prepare the array first with ordinary JavaScript: filter, sort (on a copy, [...items].sort(...), because sort changes the array it is called on) or slice. Then map the result. Keeping the data work separate from the JSX makes both easier to read.
Every element produced by map needs a key prop. Keys tell React which item is which between renders. Suppose a list shows Mango, Orange and Lychee, and Orange is deleted. With keys, React sees that the items with keys 1 and 3 are still there and removes only the item with key 2. Without them, React can only compare positions, so it may update the wrong elements, and any state inside them, such as text typed into an input, ends up next to the wrong item.
A good key is a stable id that belongs to the item: a database id, a product code, a username. It must be unique among its siblings, and must not change between renders. Two habits cause trouble. Using the array index as the key works only while the list never changes order or loses items. Generating a key with Math.random() gives a new key on every render, which makes React rebuild every item each time.
Keys are not passed on to your component as a prop; React keeps them for itself. If a child needs the id, pass it again under another name.
Example
Line by line
fruitsis the data: an array of objects, each with anid.filterprepares a second array holding only the summer fruit.- The first
mapturns every fruit into anli, with the fruit'sidas its key. - The second
maplists the filtered array; it can reuse the ids as keys, because keys only need to be unique among siblings.
Expected result
The preview shows "All fruit" with four items, starting with "Mango: 120 taka", and "In summer" with a numbered list of Mango and Lychee.
In the real world
Feeds, tables and search results are all rendered with map. Data from a server almost always has ids, which make ideal keys. Libraries that show huge lists, such as virtualised tables that render only the visible rows, rely on keys to reuse elements as you scroll.
Common mistakes
- Forgetting the key, which React reports as a warning, and which causes bugs when the list changes.
- Using the index as the key for a list that can be reordered, filtered or edited.
- Sorting the original array in place during render, which changes data that other code relies on.
- Putting the key on the wrong element: it belongs on the outermost element returned by
map.
Best practices
- Use ids from your data as keys; create them when the data is created, not during rendering.
- Prepare the array (filter, sort, slice) in variables before the return.
- Extract the JSX for one item into its own component when it grows.
Summary
Lists are rendered by mapping an array of data to JSX inside braces, after filtering or sorting it as needed. Each item needs a key that is unique among its siblings and stable between renders, ideally an id from the data, so that React can match items correctly when the list changes.
Quick check
Key takeaways
- Turn data into elements with array.map inside braces.
- Every item in a list needs a key that is unique among its siblings and stays the same between renders.
- Filter and sort the data first, then map it to JSX.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.