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.
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:
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:
<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"
{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:
{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:
function Warning({ message }) {
if (!message) return null;
return <p className="warning">{message}</p>;
}Your turn
Finish the message
Change App so that:
- When every task is done, the page says All done for today! (it already does).
- When exactly one task is left, it says 1 task left.
- 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.
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
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.
Completion is saved in this browser only. It does not measure mastery.