bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Lists

In React, you will render lists with some type of loop.

The JavaScript map() array method is generally the preferred method.

If you need a refresher on the map() method, check out the ES6 Array map() section .

Example

Let's create a simple list using the map() method:

function MyCars() {
 const cars = ['Ford', 'BMW', 'Audi'];
 return ( <> <h1>My Cars:</h1> <ul> {cars.map((car) => <li>I am a { car }</li>)}
 </ul> </> );
}
createRoot(document.getElementById('root')).render( <MyCars /> );

When you run this code in your React environment, it will work but you will receive a warning that there is no "key" provided for the list items.

Keys in React Lists

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 must be unique among siblings, but they don't have to be unique across the entire application.

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.

Example

Here the example from above, with keys:

function MyCars() {
 const cars = [ {id: 1001, brand: 'Ford'}, {id: 1002, brand: 'BMW'}, {id: 1003, brand: 'Audi'}
 ];
 return ( <> <h1>My Cars:</h1> <ul> {cars.map((car) => <li key={car.id}>I am a { car.brand }</li>)}
 </ul> </> );
}
createRoot(document.getElementById('root')).render( <MyCars /> );

Using Array Index as Keys

While it's possible to use the array index as a key, it's not recommended unless:

  • The list is static (won't change)
  • The list will never be reordered or filtered
  • The items in the list have no IDs

Example

Using array indexes as keys (not recommended for dynamic lists):

function MyCars() {
 const cars = ['Ford', 'BMW', 'Audi'];
 return ( <> <h1>My Cars:</h1> <ul> {cars.map((car, index) => <li key={index}>I am a { car }</li>)}
 </ul> </> );
}
createRoot(document.getElementById('root')).render( <MyCars /> );

Previous

React Conditional Rendering

Next

React Forms

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

Keys in React ListsUsing Array Index as Keys