Skip to content

Showing Things Conditionally

The Course
•
20 min study

Pages often show different things in different situations: a tick next to finished tasks, a message when there's nothing to show, a warning only when it's needed. In React you decide that with ordinary JavaScript. There are three common ways, and this lab uses all of them.

Three ways to choose React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Task from "./Task.jsx"; const tasks = [ { id: 1, title: "Read about props", done: true }, { id: 2, title: "Write a Card component", done: false }, ]; const reviewsDue = 0; export default function App() { const left = tasks.filter((task) => !task.done).length; return ( <main> <h1>Today</h1> <Task title={tasks[0].title} done={tasks[0].done} /> <Task title={tasks[1].title} done={tasks[1].done} /> <p>{left === 0 ? "All done for today!" : `${left} task left.`}</p> {reviewsDue > 0 && <p>You have reviews due.</p>} </main> ); } export default function Task({ title, done }) { if (done) { return <p className="muted">✓ {title}</p>; } return <p>○ {title}</p>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

1. if with an early return

Task.jsx checks done before its JSX. If the task is done, it returns one version and stops. Otherwise it carries on and returns the other:

JSX
export default function Task({ title, done }) {
  if (done) {
    return <p className="muted">✓ {title}</p>;
  }
  return <p>○ {title}</p>;
}

Use if when the two versions are quite different, or when there are more than two cases.

2. The ternary operator, condition ? a : b

if is a statement, so it can't go inside JSX braces. The ternary operator is an expression, so it can:

JSX
<p>{left === 0 ? "All done for today!" : `${left} task left.`}</p>

Use it to choose between two values in the middle of some JSX.

3. && for "this, or nothing"

JSX
{reviewsDue > 0 && <p>You have reviews due.</p>}

If the left side is false, React shows nothing. If it's true, React shows the right side. Use it when the other case is "show nothing".

Predict: the stray zero

In App.jsx, change the last condition to the shorter version below, which looks like it means the same thing:

JSX
{reviewsDue && <p>You have reviews due.</p>}

reviewsDue is 0. Predict what appears, then run it.

Check your prediction

A 0 appears at the bottom of the page. 0 && anything gives 0, and React doesn't show false, null or undefined, but it does show the number 0. Keep the comparison, reviewsDue > 0 &&, so the left side is a true or false value. This is one of the most common React surprises, so watch for it whenever the left side of && is a number.

Showing nothing

A component can return null to show nothing at all. That's useful when the component itself decides whether it's needed:

JSX
function Warning({ message }) {
  if (!message) return null;
  return <p className="warning">{message}</p>;
}

Your turn

Exercise

Finish the message

Change App so that:

  1. When every task is done, the page says All done for today! (it already does).
  2. When exactly one task is left, it says 1 task left.
  3. When more than one is left, it says, for example, 2 tasks left., with an s.

Test it by changing the done values in the tasks array.

Your working space React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Task from "./Task.jsx"; const tasks = [ { id: 1, title: "Read about props", done: true }, { id: 2, title: "Write a Card component", done: false }, ]; const reviewsDue = 0; export default function App() { const left = tasks.filter((task) => !task.done).length; return ( <main> <h1>Today</h1> <Task title={tasks[0].title} done={tasks[0].done} /> <Task title={tasks[1].title} done={tasks[1].done} /> <p>{left === 0 ? "All done for today!" : `${left} task left.`}</p> {reviewsDue > 0 && <p>You have reviews due.</p>} </main> ); } export default function Task({ title, done }) { if (done) { return <p className="muted">✓ {title}</p>; } return <p>○ {title}</p>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

A single ternary handles two cases. For three, compute the message above the return with if and else if, then show it with {message}.

Hint 2: a first step

Start with let message = "All done for today!";. Then change it when left is 1, and again when left is more than 1.

Show one solution
JSX
export default function App() {
  const left = tasks.filter((task) => !task.done).length;

  let message = "All done for today!";
  if (left === 1) {
    message = "1 task left.";
  } else if (left > 1) {
    message = `${left} tasks left.`;
  }

  return (
    <main>
      <h1>Today</h1>
      <Task title={tasks[0].title} done={tasks[0].done} />
      <Task title={tasks[1].title} done={tasks[1].done} />
      <p>{message}</p>
      {reviewsDue > 0 && <p>You have reviews due.</p>}
    </main>
  );
}

Ordinary JavaScript runs before the return. Working out a value there and showing it with braces keeps the JSX easy to read.

Was this article helpful?

Your response is saved on this device.