Most real-world React is rendering lists — users, products, messages, notifications. You already know .map() from the array methods lesson. In React, .map() turns an array of data into an array of JSX elements.
// ❌ Avoid index as key when items can be reordered or deleted{users.map((user, index) => ( <li key={index}>{user.name}</li>))}// ✅ Use the item's unique identifier{users.map(user => ( <li key={user.id}>{user.name}</li>))}
If you don’t provide a key, React will warn you in the console. If you use array index as a key and items get reordered, React will mix up which component goes with which data — causing subtle, hard-to-debug UI bugs.
The key goes on the outermost element in the .map() — which is the <UserCard> component, not any element inside it. React needs the key on the mapped element, not on a child within it.
Do filtering, sorting, and transforming above the return statement, not inside JSX. It keeps your JSX clean and readable, and you can handle the empty state separately.
This is a realistic component. Fetch data, let the user search it, render the filtered results. This pattern appears everywhere in production React apps.
// ❌ Missing key — React warns in console{users.map(user => ( <li>{user.name}</li>))}// ✅ Always provide a key{users.map(user => ( <li key={user.id}>{user.name}</li>))}
React needs keys to track which item is which across renders. Without stable keys, React can mix up item identity (especially when inserting, deleting, or reordering) and do unnecessary work.
Putting key on the wrong element
Copy
// ❌ Key is on <li> inside the component, not on the mapped element{users.map(user => ( <UserCard user={user} /> // Missing key here!))}function UserCard({ user }) { return <li key={user.id}>{user.name}</li>; // Key here does nothing}// ✅ Key goes on the element returned by .map(){users.map(user => ( <UserCard key={user.id} user={user} />))}
Forgetting parentheses in .map()
Copy
// ❌ Missing parentheses — returns undefined{users.map(user => { <li key={user.id}>{user.name}</li>})}// ✅ Use parentheses for implicit return{users.map(user => ( <li key={user.id}>{user.name}</li>))}// ✅ Or use explicit return with curly braces{users.map(user => { return <li key={user.id}>{user.name}</li>;})}
Arrow functions with {} need an explicit return. Arrow functions with () implicitly return the expression. This is a JavaScript gotcha, not a React one — but it shows up constantly in .map() calls.