0Pricing
Next.js 15 Fullstack Web Apps · Lesson

Rendering Lists, Keys, and Conditional UI

Render dynamic collections with map, choose correct keys, and show or hide UI conditionally — the everyday building blocks of React interfaces.

Rendering Lists, Keys, and Conditional UI is a free Next.js 15 Fullstack Web Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Next.js 15 Fullstack Web Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Rendering Collections

UIs constantly display lists: products, comments, search results. In React you turn an array of data into an array of elements, usually with Array.map.

Mapping an Array

Inside JSX you call map and return one element per item. React renders the resulting array directly.

const items = ['Apple', 'Banana', 'Cherry'];
function List() {
  return (
    <ul>
      {items.map((fruit) => <li>{fruit}</li>)}
    </ul>
  );
}

Why Keys Matter

React needs to track which item is which between renders. A key is a stable, unique identifier per item that lets React reuse, reorder, or remove elements efficiently instead of rebuilding everything.

Adding Keys

Pass a key prop, ideally a stable id from your data — not the array index, which breaks when items move.

{users.map((user) => (
  <li key={user.id}>{user.name}</li>
))}

The Index Key Pitfall

Using the array index as a key seems easy but causes bugs when the list is reordered or filtered: React reuses the wrong DOM node and component state attaches to the wrong item. Prefer a real unique id.

Conditional Rendering with &&

To render something only when a condition is true, use the logical AND operator. If the left side is falsy, nothing renders.

function Inbox({ count }) {
  return (
    <div>
      {count > 0 && <span>You have {count} messages</span>}
    </div>
  );
}

Ternary for Either/Or

When you must show one of two things, use a ternary expression directly in JSX.

function Status({ online }) {
  return <p>{online ? 'Online' : 'Offline'}</p>;
}

Early Returns

For larger branches, return early from the component. This keeps the main JSX clean.

function Profile({ user }) {
  if (!user) return <p>Loading...</p>;
  return <h1>{user.name}</h1>;
}

Beware the Zero Trap

Using && with a number is risky: if the value is 0, React renders the literal 0 instead of nothing. Convert to a boolean first.

{messages.length > 0 && <List />}   // safe
{messages.length && <List />}        // BUG: renders 0

Filtering and Mapping Together

You often filter then map to render a subset. Chain the array methods before returning elements.

{products
  .filter((p) => p.inStock)
  .map((p) => <Card key={p.id} product={p} />)}

Empty States

Always handle the empty case. Show a friendly message when a list has no items rather than rendering a blank area.

{items.length === 0
  ? <p>No results found</p>
  : items.map((i) => <Row key={i.id} {...i} />)}

Quick Check

Test your list-rendering knowledge.

Recap

You learned core rendering patterns:

  • Use map to turn data arrays into elements
  • Give each item a stable, unique key (not the index)
  • Render conditionally with &&, ternaries, or early returns
  • Watch the zero trap and always handle empty states

Frequently asked questions

Is the “Rendering Lists, Keys, and Conditional UI” lesson free?

Yes — the full text of “Rendering Lists, Keys, and Conditional UI” is free to read here on the web, and the Next.js 15 Fullstack Web Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Next.js 15 Fullstack Web Apps course, upgrade to CoddyKit PRO.

What will I learn in “Rendering Lists, Keys, and Conditional UI”?

Render dynamic collections with map, choose correct keys, and show or hide UI conditionally — the everyday building blocks of React interfaces. You practise Next.js 15 Fullstack Web Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Next.js 15 Fullstack Web Apps?

No prior experience is required. Next.js 15 Fullstack Web Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rendering Lists, Keys, and Conditional UI” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Next.js 15 Fullstack Web Apps lesson?

Yes. Every Next.js 15 Fullstack Web Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. React Components and JSX
  2. State Management with Hooks
  3. Props and Component Communication
  4. Rendering Lists, Keys, and Conditional UI
← Back to Next.js 15 Fullstack Web Apps