Skip to content
Core ConceptsChapter 10 / 37Beginner4 min read

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 conditionsWith conditional rendering
Everything is always on the screenOnly the relevant UI is shown
You have to hide things with CSSThe element never enters the DOM
Loading and error states are ignoredEvery state gets its own UI
The user gets confusedClear and correct feedback

3. Where is it used?

SituationWhat to show
AuthLogged in: avatar, otherwise a Login button
Data fetchA loading spinner, an error message or the data
Empty listA "No items" message
BadgesAn unread count only when it is above 0
PermissionsThe 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.

Deciding during render
The component renders
State or props are checked
The JSX is chosen based on the condition
If it is null or false, nothing is rendered
The chosen UI appears on the screen
ApproachWhen to use it
if / early returnThe whole component shows different UI
cond ? a : bOne of two options
cond && aShow it or show nothing
return nullThe component renders nothing

5. Code example

Early return

jsx
function Profile({ user, isLoading }) {
  if (isLoading) {
    return <p>Loading...</p>;
  }

  if (!user) {
    return <button>Login</button>;
  }

  return <h2>Welcome, {user.name}</h2>;
}

Ternary and &&

jsx
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?

PartMeaning
if (isLoading) returnHandle 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, undefinedReact 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

Final quick revision

PointDetail
Special syntaxNone, just plain JavaScript
Two optionsTernary ? :
Show or not&& (the left side must be a boolean)
A completely different UIEarly return
Nothingreturn null