In React, lists are rendered using some form of loop, with the JavaScript map() array method being the preferred approach.
If you need a refresher on the map() method, refer to the ES6 section. |
Let’s display all the cars from our garage.
function |
When you run this code in your create-react-app, it will work but you’ll receive a warning indicating that the list items are missing a “key” prop.
Keys help React track elements so that only updated or removed items are re-rendered, and they must be unique among siblings but can be duplicated globally.
Ideally, the key should be a unique ID for each item; as a last resort, you can use the array index as a key. |
Let’s update our previous example to include keys.
function |