Conditional Rendering
โš›๏ธ React 18+ ๐ŸŸข Chapter 9 of 39 ๐Ÿ“‚ Phase 04: Props and Rendering ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: if Statements ยท Ternary Operator ยท Logical && ยท Loading UI ยท Empty/Error States ยท Multiple Conditions
Real interfaces constantly need to show different content based on data โ€” a loading spinner, an empty state, a logged-in vs logged-out view. React handles all of this using plain JavaScript, no special syntax required.
1Ternary Operator for Two-Way Choices
๐Ÿ’ป Example 1: Ternary Conditional Rendering
function Status({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
    </div>
  );
}

The pattern condition ? ifTrue : ifFalse is the standard way to choose between exactly two pieces of JSX to render.

2Logical && for Show-or-Nothing Cases
function Notifications({ count }) {
  return (
    <div>
      <h3>Inbox</h3>
      {count > 0 && <p>You have {count} new messages</p>}
    </div>
  );
}

condition && <JSX /> renders the JSX only when the condition is true, and renders nothing at all when it's false โ€” perfect for optional content like badges or warning banners.

3Loading, Empty, and Error States
function UserList({ loading, error, users }) {
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Something went wrong.</p>;
  if (users.length === 0) return <p>No users found.</p>;

  return (
    <ul>
      {users.map(u => <li key={u.id}>{u.name}</li>)}
    </ul>
  );
}

Using early return statements for each distinct state (loading, error, empty, success) keeps a component's main render logic clean, rather than nesting several ternaries inside one another.

โš ๏ธ Using && with a Number That Could Be Zero

Writing {count && <p>...</p>} when count could be 0 has a sneaky bug: since 0 is falsy, React actually renders the literal number 0 on the page (because 0 && anything evaluates to 0, and React renders numbers). Fix it by explicitly comparing: {count > 0 && <p>...</p>}.

๐Ÿ’ป Hands-on Interactive Practice Challenge

Write a component that shows "Loading...", an error message, or a list of items depending on three boolean/array props, using early returns.

React Practice Challenge โ–ถ Run in Compiler
function ItemList({ loading, error, items }) {
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Failed to load items.</p>;

  return (
    <ul>
      {items.map((item, i) => <li key={i}>{item}</li>)}
    </ul>
  );
}
Run This Challenge in Online JS IDE โ†’
โ“ Frequently Asked Questions (FAQ)

Q Which is better: ternary or &&?

Use && when you only need to show something or nothing (one-sided). Use a ternary when you're choosing between two different pieces of JSX to display โ€” both are equally valid, standard React patterns.

Q Can I use a regular if statement directly inside the JSX return?

Not directly inside curly braces, since if is a statement, not an expression. You can use if statements above the return (as shown with early returns), or use a ternary/&& inside the JSX itself.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on React 18+ ยท Last updated August 2026