Conditional Rendering — React

Conditional rendering means a component shows different things depending on the situation, using plain JavaScript. The ternary condition ? A : B picks one of…

Open a ride-hailing app before you log in: you see a "Sign in" button. After you log in, the same spot shows your name and photo. Same page, different content depending on the situation.

In React this is called conditional rendering: a component shows different things depending on the situation. Logged out it shows "Please log in.", logged in it shows "Welcome!". There's no special way of writing it: just plain JavaScript, which means if, the ternary, and &&. It's like the sign on a shop door: it reads OPEN or CLOSED depending on the situation. Sometimes there's also a "SALE" sticker that is only put up when there really is a sale. No sale, no sticker. The OPEN or CLOSED sign is like the ternary: one of the two words is always there. The SALE sticker is like &&: it shows, or it isn't there at all.

The ternary: pick one of two

A ternary is written condition ? A : B. The condition is a value that counts as true or false: when the condition is true the result is A, when it's false the result is B. Because a ternary is an expression (code that produces a value), it can go inside { } in JSX. In this code, the condition is the variable isLoggedIn:

function App() {
  const isLoggedIn = true;

  return (
    <div>
      {isLoggedIn ? <p>Welcome!</p> : <p>Please log in.</p>}
    </div>
  );
}
isLoggedIn What renders
true <div><p>Welcome!</p></div>
false <div><p>Please log in.</p></div>

Use a ternary when there are two views and one of them always shows.

&&: shown, or not there at all

Sometimes there's only one view: it shows when the condition is true, and otherwise there's nothing. For that, use &&:

function App() {
  const items = ["A", "B", "C"];

  return (
    <div>
      {items.length > 0 && <p className="count">Total: {items.length}</p>}
    </div>
  );
}

Remember from JavaScript: a && b gives a when a is falsy, and b when a is truthy.

items items.length > 0 Result of && Shows
["A", "B", "C"] true the <p> Total: 3
[] false false (nothing)

React shows nothing for false, null, undefined and true. So when the condition is false, the <p> disappears completely. The number in Total: {items.length} is read from the array too, so when items are added, the number changes with it.

Watch out for 0 in &&

Now the condition is just items.length, with no > 0, and items is empty. Before you read on, take a guess: is the page empty, or does something show?

{items.length && <p className="count">Total: {items.length}</p>}

items.length is 0. 0 is falsy, so && stops there and gives 0, not false. The problem: React shows numbers as text, 0 included. So the page shows 0 on its own. The same happens with {0 && <p>Hello</p>}: what shows is the number 0.

The fix is to make sure the left side of && is always true or false:

if above return

if is a statement (a command that produces no value), not an expression, so it can't go inside { } in JSX. Its place is above the return. Use it when one situation replaces the component's whole content, for example a user who isn't logged in:

function Profile({ user }) {
  if (!user) {
    return <p>Please log in.</p>;
  }

  return <h1>Hello, {user.name}!</h1>;
}

When there's no user yet, !user is true, and the function stops at the first return. This is often called an early return (returning early). So there are three ways to render conditionally: if (above return), the ternary ? :, and &&.

🎯 Which one to pick?

⚠️ A stray 0 and a missing branch

TL;DR: Conditional rendering means a component shows different things depending on the situation, using plain JavaScript. The ternary condition ? A : B picks one of two views: A when the condition is true, B when it's false. condition && A shows A or nothing, because React shows nothing for false, null, undefined or true. But {0 && <p>Hello</p>} gives 0, and React shows numbers, 0 included. So {items.length && ...} with an empty list also shows 0. Write items.length > 0 && .... if can't go inside JSX, so put the if above return (an early return: the function stops at the first return). Three ways: if, the ternary, and &&.

⚡ The ternary: pick one of two

Write:

{isLoggedIn ? <p>Welcome!</p> : <p>Please log in.</p>}

Meaning: Showing different things depending on the situation is called conditional rendering. The ternary condition ? A : B picks A when the condition is true and B when it's false. Here the condition is isLoggedIn (logged in or not), so only one of the two <p> elements shows. The one not picked isn't hidden: it's simply never made.

⚡ Watch out for 0 in &&

Write:

{items.length && <p className="count">Total: {items.length}</p>}
Guess first: when the array items is empty, is the page empty, or does something show?

The number 0 shows. items.length is 0, and 0 is falsy (it counts as false), so && stops there and gives 0. React shows numbers, 0 included.

Meaning: condition && A shows A or nothing. Make sure the left side of && is always true or false: write items.length > 0 && ....

⚡ if above return

Write:

function Profile({ user }) {
  if (!user) {
    return <p>Please log in.</p>;
  }

  return <h1>Hello, {user.name}!</h1>;
}

Meaning: if is a statement (a command that produces no value), so it can't go inside { } in JSX. Its place is above return. When there's no user yet, the function stops at the first return: this is called an early return. So there are three ways to render conditionally: if, the ternary, and &&.

What you will learn