Introduction
Interfaces change with their data: a signed-out visitor sees a sign-in prompt, an empty inbox says so, a badge appears only when there is something unread. React has no special syntax for this. You use ordinary JavaScript conditions to decide which JSX a component returns, and this lesson shows the four patterns that cover almost every case.
How it works
An early return handles a whole alternative view. At the top of the component, check for the special case and return different JSX:
if (!user) {
return <p>Please sign in.</p>;
}
The rest of the component then deals only with the normal case, which keeps it simple.
The conditional operator condition ? a : b picks between two pieces of JSX inside braces, because it is an expression: {messages.length === 0 ? <p>No messages yet.</p> : <MessageList />}.
The && operator shows something or nothing: {unread > 0 && <Badge />}. JavaScript evaluates a && b to a when a is falsy, and to b otherwise. React shows nothing for false, null and undefined, so the badge disappears when the condition is false. But React does show numbers, including 0, so {count && <Badge />} shows a stray 0 when the count is zero. Always make the left side a real boolean, such as count > 0.
Returning null makes a component show nothing at all. A Warning component can return null when there is no warning, so the parent can always render it without checking first.
For more than two cases, compute the JSX in a variable before the return with if ... else if, or use a switch, or pick a component from an object keyed by status. Whatever the choice, the condition is plain JavaScript; JSX only shows the result.
Example
Line by line
- With no user,
Inboxreturns the sign-in prompt early and stops. unreadcounts the messages not yet read.unread > 0 && ...shows the badge only when there is something unread, and never a 0.- The conditional operator chooses between "No messages yet." and the total.
Appshows the three situations side by side.
Expected result
The preview shows "Please sign in.", then Ada's inbox with the badge "1 unread" and "2 messages in total", then Alan's inbox with no badge and "No messages yet."
In the real world
Every real interface has states: loading, empty, error, success, signed in or out, with or without permission. Design teams draw each state, and developers render each one with the patterns from this lesson. A common bug report, a lonely "0" appearing on a page, comes straight from the && trap.
Common mistakes
- Writing count && ..., which shows 0 when the count is zero.
- Putting an if statement inside JSX braces, which is a syntax error.
- Nesting conditional operators deeply, which is hard to read; use an early return or a variable.
- Hiding with CSS when the element should not exist, which leaves it in the page for screen readers.
Best practices
- Handle special cases first with early returns.
- Keep conditions short in JSX; name complex ones in a variable, such as
const showBadge = unread > 0. - Let a component return null when it has nothing to show, instead of making every parent check.
Summary
Conditional rendering uses plain JavaScript: early returns for alternative views, ? : to pick between two pieces of JSX, && to show something or nothing (with a real boolean on the left, to avoid a stray 0), and null to show nothing at all.
Quick check
Key takeaways
- Use if with an early return for whole alternative views.
- Use ? : to pick between two pieces of JSX, and && to show something or nothing.
- With &&, make the left side a real boolean: count > 0, not count, or React will show 0.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.