Stop 10 · React BasicsTopics · 9/11

Conditional Rendering and Lists

Part of the Web Development Roadmap

In short

Almost every screen is either 'show this or that' or 'show one of these for every item'. Loading spinners, empty carts, product grids: they're all conditional rendering and lists.

Conditional rendering shows different UI based on state, using if, the ternary operator or &&. Lists are rendered with map, and each item needs a stable, unique key.

The jaadu stands under a big railway departure board with rows of flip tiles, pointing up at one row marked with a bright red tag.

Think of it like this

The railway departure board

A station board shows one row for every train, built from the timetable. A delayed train gets a red 'Late' tag; an on-time one doesn't. If there are no more trains tonight, the whole board just says 'No departures'. That's lists with map, plus conditions deciding what to show.

Watch it happen

What the screen shows as data loads

loading
error?
empty?
list

1/4While loading is true, return a 'Loading...' message and nothing else.

The big ideas

What you need to know

Three ways to choose

An early return with if for big cases, cond ? a : b for either-or, and cond && <X /> for show-or-nothing.

map makes lists

items.map((item) => <Row key={item.id} item={item} />) turns an array of data into an array of elements.

Keys must be stable and unique

Use an id from your data. React uses keys to track which item is which when the list changes.

Watch out for 0

count && <Badge /> shows a literal 0 when count is 0. Use count > 0 && <Badge /> instead.

See it in code

Walk through the code

jsx
1function OrderList({ orders, loading, error }) {2  if (loading) return <p>Loading orders...</p>;3  if (error) return <p className="error">Could not load orders.</p>;4 5  if (orders.length === 0) {6    return <p>No orders yet. Time for some samosas?</p>;7  }8 9  return (10    <ul>11      {orders.map((order) => (12        <li key={order.id}>13          {order.item}: ₹{order.amount}14          {order.status === "delivered" ? " ✓" : " (on the way)"}15          {order.isNew && <span className="tag">New</span>}16        </li>17      ))}18    </ul>19  );20}

1/5Early returns handle loading and error first, so the rest of the code stays simple.

Every common pattern in one component: early returns, an empty state, a list with keys, a ternary and &&.

Save yourself the pain

Common mistakes

Oops

Using the array index as the key in a list that can be reordered or filtered.

Do this instead

Use a real id. Index keys can make React mix up items, for example keeping a checkbox ticked on the wrong row.

Oops

Writing if statements directly inside JSX curly braces.

Do this instead

JSX braces take expressions. Use a ternary or &&, or move the if above the return.

Your mission

Student list with states

~25 min

Quick check

What renders when cart.length is 0 in {cart.length && <p>Items in cart</p>}?

Go deeper

  1. 1Show a loading message, an error or the data depending on state.
  2. 2Render a list of tasks with map and use each task's id as the key.
  3. 3Learn why using the array index as a key can cause bugs when items move.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge