Skip to main content

Arrays become lists

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.
That’s the entire pattern: .map() over an array, return JSX for each item, add a key prop. You’ll use this in virtually every React component.

The key prop

Every item in a list needs a unique key prop. React uses keys to track which items changed, were added, or were removed.

What makes a good key?

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.

Rendering components from a list

Usually you render a component for each item, not raw HTML:
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.

Filtering and transforming before rendering

Process your data before the return statement. Don’t try to do complex logic inside JSX:
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.

A searchable list — putting it together

This combines state, effects, list rendering, and conditional rendering:
This is a realistic component. Fetch data, let the user search it, render the filtered results. This pattern appears everywhere in production React apps.

Common mistakes

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.
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.

What’s next?

You can render lists of data. Now let’s learn how to handle user input — forms are how users interact with your app.

Forms in React

Build controlled forms that sync input values with React state