In React JSX, you render lists from arrays using .map() to transform each array item into a JSX element (or component), and attach a unique, stable key prop to each top-level item returned by .map(). Avoid for loops or .forEach() directly inside JSX expressions because JSX expects expressions that return values or arrays of elements, not statements or undefined.


1. Rendering Lists with .map() and Unique key Props

Pass a callback to array.map((item) => ...) inside { ... } braces in JSX so each item becomes a rendered element with a stable key:

// Rendering an array of items in a React component using .map() and a stable key
function TaskList({ tasks }) {
  return (
    <ul>
      {tasks.map((task) => (
        <li key={task.id}>
          <span>{task.title}</span>
          <strong>{task.completed ? "Done" : "Pending"}</strong>
        </li>
      ))}
    </ul>
  );
}

const sampleTasks = [
  { id: "t-101", title: "Review PR", completed: true },
  { id: "t-102", title: "Write unit tests", completed: false }
];

console.log(typeof TaskList, sampleTasks.length);
  • .map() returns a new JSX array: Each iteration returns an element (or component) that React renders in order.
  • Stable key on the outer returned element: Keys help React identify which items were added, removed, or reordered across updates.
  • Extract child components for clarity: When each row or card has extra logic or events, rendering <TaskItem key={task.id} task={task} /> keeps the parent list readable.

2. Filtering, Sorting, and Handling Empty Lists

You can chain .filter() or .sort() before .map() (copying first if sorting so you do not mutate props/state) and render a fallback when the array is empty:

// Filtering active items and handling an empty array cleanly in React JSX
function ActiveMemberList({ members }) {
  const activeMembers = members.filter((member) => member.active);

  if (activeMembers.length === 0) {
    return <p>No active members found.</p>;
  }

  return (
    <ul>
      {activeMembers.map((member) => (
        <li key={member.id}>{member.name}</li>
      ))}
    </ul>
  );
}

console.log(typeof ActiveMemberList);
  • Filter before .map(): Narrowing items first avoids creating unnecessary JSX elements for hidden entries.
  • Explicit empty state: Checking .length before mapping gives users clear feedback instead of rendering a blank container.
  • Avoid mutating arrays in place: Copy arrays ([...items].sort(...)) before sorting inside render logic so state stays predictable.

3. Common Traps to Avoid

List rendering in React is straightforward once you watch out for a few recurring mistakes:

  • Using array index (index) as key on dynamic lists: Index keys can cause wrong input values, misplaced focus, or stale UI state when items are inserted, deleted, or reordered—always prefer a unique ID (item.id).
  • Using .forEach() inside JSX: .forEach() returns undefined, so pushing into an external array inside JSX will not render the items as expected.
  • Missing key on <Fragment> inside .map(): If each mapped item returns multiple sibling elements wrapped in <Fragment>, write <Fragment key={item.id}>...</Fragment> (shorthand <>...</> cannot take a key).

TL;DR: Use array.map((item) => <Item key={item.id} ... />) inside JSX to render lists cleanly, using a unique stable key on each top-level element and handling empty arrays explicitly.

Are you rendering simple strings/objects, filtering or sorting dynamic lists, or dealing with a specific key or component issue?