Sub topic 11
Showing or hiding with && and the 0 pitfall
This chapter assumes you know JavaScript arrays and objects, React function components, props, JSX, and the useState hook. You should also have seen if and the ternary operator used for conditional rendering. Here you will learn when && is the right tool and how it can print a stray 0 on the screen.
How && works in JSX
JavaScript's && operator evaluates the left side first. If the left side is falsy, && returns it and stops. If the left side is truthy, && returns the right side.
React renders whatever the expression produces. The right side is usually JSX, so a truthy condition shows the element. A falsy condition produces a value that React may or may not skip, and that detail causes the pitfall below.
function Message({ isAdmin }) {
return (
<div>
<p>Welcome back.</p>
{isAdmin && <button>Delete all</button>}
</div>
);
}When isAdmin is true, the button appears. When it is false, nothing appears.
When to choose && over a ternary
Use && when there is only one outcome: show something or show nothing. If you need two different outputs, use a ternary instead. A ternary written as condition ? <A /> : null does the same job as && but is longer, so && is the shorter choice for a single branch.
| Situation | Pattern |
|---|---|
| Show an element or nothing | `condition && <A />` |
| Show one of two elements | `condition ? <A /> : <B />` |
| Return early from a component | `if (...) return ...` |
The 0 pitfall
React skips false, null, undefined, and true when rendering. It does not skip the number 0. React renders 0 as text.
So if the left side of && is a number, and that number is 0, then && returns 0 and React prints it.
function Inbox({ unreadCount }) {
return (
<div>
{/* Renders the number 0 when unreadCount is 0 */}
{unreadCount && <span>{unreadCount} unread</span>}
{/* Renders nothing when unreadCount is 0 */}
{unreadCount > 0 && <span>{unreadCount} unread</span>}
</div>
);
}With unreadCount set to 0, the first line puts a literal 0 in the page. The second line compares first, so the left side is false, and nothing is drawn.
The same problem applies to NaN, which React also prints. An empty string "" renders as nothing visible, but the comparison habit protects you from all of these cases.
Following the logic
Using && in a real component
The Todos component from the spine uses && for a small badge. The left side is todo.done, which is always a boolean, so it is safe.
<li key={todo.id}>
{todo.title}
{todo.done && <span> (done)</span>}
</li>Because done is true or false, no 0 can leak out. The risk appears when the left side comes from a count, a length, or a value from an API that might be a number.
Common errors
- Writing
{items.length && <List items={items} />}. An empty array prints0. Writeitems.length > 0 &&instead. - Putting the whole expression in parentheses and expecting that to fix it. Parentheses do not change what the left side returns.
- Using
&&for two outcomes by chaining it twice. Use a ternary instead, so the intent is clear.
Exercise
A component renders {messages.length && <MessageList messages={messages} />}. What appears on screen when messages is an empty array, and how do you fix it?
Show answer
The page shows the text 0, because messages.length is 0, which is falsy, so && returns 0 and React renders numbers. Fix it by comparing first: {messages.length > 0 && <MessageList messages={messages} />}. Using Boolean(messages.length) also works.
Exercise
You want to show <Spinner /> while loading is true and <Results /> otherwise. Should you use && or a ternary?
Show answer
Use a ternary: {loading ? <Spinner /> : <Results />}. There are two outputs, and && only handles the show-or-nothing case.