Course contentsShow
← React
Lesson 123 of 2,3964. Conditional Rendering in JSXFree lesson

Ternary with null for One-Sided Conditionals

Use condition ? <Element /> : null when you only need to render on true, though && is often cleaner here.

Ternary with null for One-Sided Conditionals

What you'll learn: How to use the ternary operator with null when you only want to render something conditionally, but have no "else" case.

The Core Idea

Sometimes you want to show a component only if a condition is true, with nothing rendered when it's false. You've already learned condition && <Element /> for this, but the ternary operator offers an alternative: condition ? <Element /> : null.

This is the ternary's way of saying "if true, render this; if false, render nothing."

When This Pattern Appears

function UserProfile({ isPremium }) {
  return (
    <div>
      <h1>Welcome!</h1>
      {isPremium ? <PremiumBadge /> : null}
    </div>
  );
}

Here, the badge only appears when isPremium is true. When false, the ternary returns null, which React renders as nothing—just like undefined, true, or false.

Why null Specifically?

Because null is React's explicit "render nothing" signal. It's clearer than leaving the false branch empty or using undefined.

&& vs ? : null

For one-sided conditionals, && is usually cleaner and more idiomatic. Save ? : null for situations where you're already using ternaries nearby and want visual consistency, or when you're refactoring from a full if-else and removing the "else" case.

Key Takeaway: condition ? <Element /> : null works for one-sided conditionals, but && is the more common React pattern for this exact scenario—though both are valid and do the same thing.