How do you render a list in React, and why do you need a key?

React JSX, components and props · Freshers

Model answer

Use map on the array and return an element for each item: items.map((item) => <li key={item.id}>{item.name}</li>). The key is a special prop that gives each item a stable identity so React can update the right element when the list changes.

Use a unique, stable value such as a database id, not a random number generated on each render.

Learn this topic

React Components and Props Explained with Examples in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions