React Lists
React Lists
React Lists
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
React Lists
❮ Previous Next ❯
If you need a refresher on the map() method, check out the ES6 section.
Example:
Let's render all of the cars from our garage:
function Car(props) {
return <li>I am a { props.brand }</li>;
}
function Garage() {
const cars = ['Ford', 'BMW', 'Audi'];
return (
<>
<h1>Who lives in my garage?</h1>
<ul>
{cars.map((car) => <Car brand={car} />)}
https://www.w3schools.com/REACT/react_lists.asp 1/8
4/2/25, 10:44 a.m. React Lists
</ul>
Tutorials
</> Exercises Services Sign Up Log in
);
HTML
} CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Run Example »
When you run this code in your create-react-app , it will work but you will receive a
warning that there is no "key" provided for the list items.
Get Certified!
school
w3 s
5
CE
02
TI 2
Complete the React modules, do the exercises, take the exam and become
R
FI .
ED
w3schools certified!!
$95 ENROLL
Keys
Keys allow React to keep track of elements. This way, if an item is updated or removed,
only that item will be re-rendered instead of the entire list.
Keys need to be unique to each sibling. But they can be duplicated globally.
Generally, the key should be a unique ID assigned to each item. As a last resort, you can
use the array index as a key.
https://www.w3schools.com/REACT/react_lists.asp 2/8
4/2/25, 10:44 a.m. React Lists
Tutorials
Example:
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Let's refactor our previous example to include keys:
function Car(props) {
return <li>I am a { props.brand }</li>;
}
function Garage() {
const cars = [
{id: 1, brand: 'Ford'},
{id: 2, brand: 'BMW'},
{id: 3, brand: 'Audi'}
];
return (
<>
<h1>Who lives in my garage?</h1>
<ul>
{cars.map((car) => <Car key={car.id} brand={car.brand} />)}
</ul>
</>
);
}
Run Example »
?
Exercise
Which statement is NOT correct:
https://www.w3schools.com/REACT/react_lists.asp 3/8
4/2/25, 10:44 a.m. React Lists
Keys can
Tutorials
beExercises
the array index Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Submit Answer »
❮ Previous Next ❯
ADVERTISEMENT
https://www.w3schools.com/REACT/react_lists.asp 4/8
4/2/25, 10:44 a.m. React Lists
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_lists.asp 5/8
4/2/25, 10:44 a.m. React Lists
COLOR PICKER
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
ADVERTISEMENT
https://www.w3schools.com/REACT/react_lists.asp 6/8
4/2/25, 10:44 a.m. React Lists
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
Java Tutorial
Tutorials
C++ Tutorial
Exercises Services Java Reference
Angular Reference
Sign Up Log in
jQuery Tutorial jQuery Reference
HTML
CSS TopJAVASCRIPT
Examples SQL PYTHON JAVA
Get PHP
Certified HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_lists.asp 8/8