Conditional Rendering
Learn React conditional rendering with if, the ternary and the && operator to show or hide UI, plus the 0 bug and returning null.
On this page
1. What is conditional rendering?
React has no special syntax for this. You use normal JavaScript: if, the ternary ? : and logical &&.
2. Why does it matter?
A real UI is never the same all the time. If the user is logged in, show a profile, otherwise show a login button. While data is loading, show a spinner.
| Without conditions | With conditional rendering |
|---|---|
| Everything is always on the screen | Only the relevant UI is shown |
| You have to hide things with CSS | The element never enters the DOM |
| Loading and error states are ignored | Every state gets its own UI |
| The user gets confused | Clear and correct feedback |
3. Where is it used?
| Situation | What to show |
|---|---|
| Auth | Logged in: avatar, otherwise a Login button |
| Data fetch | A loading spinner, an error message or the data |
| Empty list | A "No items" message |
| Badges | An unread count only when it is above 0 |
| Permissions | The Delete button only for admins |
4. How does it work?
JSX is also a JavaScript value. You can store it in a variable, return it from a function and choose it with conditions.
| Approach | When to use it |
|---|---|
if / early return | The whole component shows different UI |
cond ? a : b | One of two options |
cond && a | Show it or show nothing |
return null | The component renders nothing |
5. Code example
Early return
function Profile({ user, isLoading }) {
if (isLoading) {
return <p>Loading...</p>;
}
if (!user) {
return <button>Login</button>;
}
return <h2>Welcome, {user.name}</h2>;
}Ternary and &&
function Inbox({ messages, isOnline }) {
return (
<div>
<span>{isOnline ? "Online" : "Offline"}</span>
{messages.length > 0 && <p>{messages.length} new messages</p>}
</div>
);
}How does the code work?
| Part | Meaning |
|---|---|
if (isLoading) return | Handle special cases first, so the rest of the code stays clean |
isOnline ? "Online" : "Offline" | One of two values |
messages.length > 0 && ... | The paragraph renders only if the condition is true |
null, false, undefined | React does not render these |
6. Common mistakes
Mistake: Putting a number directly on the left of &&, like messages.length && ...
Why? If the length is 0, React shows 0 as text on the screen, because 0 is a valid value to render.
Better: Always make it a boolean: messages.length > 0 && ... or use a ternary.
Mistake: Writing an if statement inside JSX
Why? Only expressions work inside JSX curly braces. An if statement is a syntax error.
Better: Write the if before the return, or use a ternary or && inside the JSX.
Mistake: Using too many nested ternaries
Why? Code like a ? b : c ? d : e is hard to read and hard to debug.
Better: Move the logic into a variable or small components, or use early returns.
Interview tip
If you are asked what a component should return to render nothing, the answer is null. The component function still runs, it just produces empty output.
7. What to learn before and after
| Stage | Chapters |
|---|---|
| Before | JSX, State with useState |
| After | Lists & Keys, Suspense & lazy() |
| Related | Error Boundaries |
Final quick revision
| Point | Detail |
|---|---|
| Special syntax | None, just plain JavaScript |
| Two options | Ternary ? : |
| Show or not | && (the left side must be a boolean) |
| A completely different UI | Early return |
| Nothing | return null |