React Lists & Keys
We can use the JavaScript's map() methodto create lists.
React example
Using the map() method to iterate over an array generates a list of numbers from 1 to 5:
const
numbers
=
[
1
,
2
,
3
,
4
,
5
]
;
const
listItems
=
numbers
.
map
(
(
numbers
)
=> <
li
{
numbers
}
li
)
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<ul{listItems}ul)>;
Try it »
We can refactor the above example into a component that receives an array as a parameter, and assign a key to each list item; otherwise a warning "a key should be provided for list items" will appear, meaning a key needs to be included:
React example
function
NumberList
(
props
)
{
const
numbers
=
props
.
numbers
;
const
listItems
=
numbers
.
map
(
(
number
)
=> <
li
key
=
{
number
.
toString
(
)
}
{
number
}
li
)
;
return
(
<ul{listItems}ul)>;
}
const
numbers
=
[
1
,
2
,
3
,
4
,
5
]
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
NumberList
numbers
=
{
numbers
}
/>
);
Try it »
Keys
Keys help React identify which elements have changed when elements in the DOM are added or removed. Therefore, you should give each element in the array a definite identity.
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
{number}
);
An element's key is best as a unique string that the element has in the list. Usually, we use the id from the data as the element's key:
const todoItems = todos.map((todo) =>
{todo.text}
);
When an element has no definite id, you can use its serial number index as the key:
const todoItems = todos.map((todo, index) =>
// Only use when id is not determined
{todo.text}
);
If the list can be re-sorted, we do not recommend using the index as the key, because this will make rendering very slow.
Extracting components with keys
An element's key only has meaning when compared with its siblings.
For example, if you extract a ListItem component, you should keep the key on this
Incorrect example
function ListItem(props) {
const value = props.value;
return (
// Oops! You don't need to specify the key here:
{value}
);
}
function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
//Wrong! The element's key should be specified here:
);
return (
{listItems}
);
}
const numbers = [1, 2, 3, 4, 5];
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
);
The correct way to use key
React example
function
ListItem
(
props
)
{
//
Correct! There is no need to specify a key here:
return
<
li
{
props.
value
}
li
;
}
function
NumberList
(
props
)
{
const
numbers
=
props
.
numbers
;
const
listItems
=
numbers
.
map
(
(
number
)
=>
//
Correct again! The key should be specified in the context of the array
<
ListItem
key
=
{
number.
toString
(
)
}
value
=
{
number
}
/>
)
;
return
(
<ul{listItems}ul)>;
}
const
numbers
=
[
1
,
2
,
3
,
4
,
5
]
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
NumberList
numbers
=
{
numbers
}
/>
);
Try it »
When you call elements inside the map() method, you'd better always remember to add a unique key to each element.
An element's key should be unique among its siblings
Keys used in array elements should be unique among their siblings. However, they do not need to be globally unique. When we generate two different arrays, we can use the same keys.
React example
function
Blog
(
props
)
{
const
sidebar
=
(
<
ul
{
props.
posts
.
map
(
(
post
)
=> <
li
key
=
{
post
.
id
}
{
post
.
title
}
li
)
}
ul
)
;
const
content
=
props
.
posts
.
map
(
(
post
)
=> <
div
key
=
{
post
.
id
}
< h3 { post.
title
}
h3
< p { post.
content
}
p
div
)
;
return
(
<div{sidebar}> <hr/>{content}div)>;
}
const
posts
=
[
{
id
:
1
,
title
:
'
Hello World
'
,
content
:
'
Welcome to learning React!
'
}
,
{
id
:
2
,
title
:
'
Installation
'
,
content
:
'
You can install React from npm.
'
}
]
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
< Blog posts = { posts } /> );
Try it »
The key serves as a hint to React but is not passed to your component. If your component needs to use the same value as the key, pass it as a prop:
const content = posts.map((post) =>); In the example above, the Post component can read props.id but not props.key.
Embedding map() in JSX
In the example above, we declared a separate listItems variable and included it in JSX:
function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) =>); return ( {listItems}
); }JSX allows any expression to be embedded in curly braces, so we can use it inside map() like this:
React example
function
NumberList
(
props
)
{
const
numbers
=
props
.
numbers
;
return
(
< ul { numbers.
map
(
(
number
)
=> <
ListItem
key
=
{
number
.
toString
(
)
}
value
=
{
number
}
/>
)
}
ul
)
;
}
Try it »
Doing this can sometimes make your code clearer, but sometimes this style can be abused. Just as in JavaScript, whether to extract a variable for readability is entirely up to you. But remember, if a map() is nested too many levels, you can extract a component.