React Reference ยท JSX Syntax and Rules
Conditional rendering in JSX
Reference for conditional rendering in JSX: the && operator, ternaries, early returns and variables, the 0 pitfall, and how conditions affect component state.
JSX has no if tag or v-if attribute. Conditional rendering is ordinary JavaScript: you decide which element to produce, and React renders whatever you return. Because null, undefined, true and false are valid children that render nothing, "show this only when..." is easy to express in several ways.
There are four common patterns: the logical AND (cond && <X />), the ternary (cond ? <A /> : <B />), an early return from the component (if (!user) return null;), and assigning an element to a variable before the return. They produce the same result; the choice is about readability.
function Status({ online }) { return <p>{online ? 'Online' : 'Offline'}</p>; }
Try it
Syntax
{condition && <Element />} // element or false
{condition ? <A /> : <B />} // one of two
{condition ? <A /> : null} // element or nothing
if (condition) return <A />; // early return, before the main JSX
| Pattern | Notes |
|---|---|
a && b |
Evaluates to a if a is falsy, otherwise to b. React ignores false, null, undefined and true, but renders 0 and NaN, and an empty string renders nothing visible. |
a ? b : c |
Always yields b or c. Use null for the empty branch. Nested ternaries are hard to read; extract a variable or a component. |
Early return |
Keeps the happy path unindented. Hooks must be called before any early return. |
| Variable | Assign in if/else blocks, then place in the markup. |
Returns: the chosen element or nothing. A component can return null to render nothing.
Usage notes
- The
0pitfall. If the left side of&&is a number,0is rendered as text. Convert it to a boolean:count > 0 && ...orBoolean(count) && .... - Hooks and early returns. Never put a hook after a conditional
return. React calls hooks in the same order on every render; skipping one breaks it. Return after all hooks. - Position decides identity. The same component type in the same position keeps its state when the condition changes:
cond ? <Counter label="A" /> : <Counter label="B" />shares one counter. Add akey(<Counter key="A" />) to reset it, or use&&on separate branches to unmount one. - Different types reset state. If the branches are different component types or elements (
<div>versus<section>), React unmounts one and mounts the other. - Do not define components inside a condition. A component created during render is a new type each time, so it remounts on every render.
- Hiding versus removing. Conditional rendering removes the node. For toggling visibility while keeping state, hide with CSS (
style={{ display: 'none' }}or thehiddenattribute). The same trick preserves scroll position and input text. switchand lookup objects. For many cases, use aswitchstatement beforereturn, or an object that maps values to elements:const views = { list: <List />, grid: <Grid /> };.- Server data. Check loading and error states first, then the data, so the later branches can assume a value exists. For async trees,
Suspenseremoves much of this branching.