Conditional Rendering in React: Six Practical Patterns

React components decide what to show with plain JavaScript. See six conditional rendering patterns, from if statements to ternaries, and when to use each.

6 min read

Conditional rendering in React means deciding what a component shows based on its data. Because JSX compiles to JavaScript, there is no special template syntax. You use the same if statements, ternaries, and logical operators you already know.

A component can show a packed label for one item and a plain label for another. Here is that idea as a ternary:

App.jsxApp.jsx
function Item({ name, isPacked }) {
  return <li>{isPacked ? name + " (packed)" : name}</li>;
}

When isPacked is true, the list item reads "Space suit (packed)." When it is false, the item reads only the name. The condition lives in the markup, and React renders one of the two results.

Six patterns for conditional rendering

Each pattern below solves the same problem in a slightly different way. They all produce valid JSX, so choose the one that stays readable as the condition grows.

1. Early return with if

The simplest pattern returns the unusual case first and lets the rest of the function run only when the condition passes.

App.jsxApp.jsx
function Status({ message }) {
  if (!message) {
    return <p>No status yet.</p>;
  }
 
  return <p>{message}</p>;
}

When message is empty, the page shows "No status yet." Otherwise it shows the message. The early return keeps the fallback separate from the main output.

2. if and else returning different JSX

When both branches are equally important, write them side by side with if and else.

App.jsxApp.jsx
function Item({ name, isPacked }) {
  if (isPacked) {
    return <li>{name} (packed)</li>;
  }
  return <li>{name}</li>;
}

The packed item gets a suffix, and the rest stay plain. This reads clearly, but it duplicates the surrounding list item tag.

3. Ternary operator

A ternary condenses an either or choice into one expression. Use it when both branches fit on one line and you want to stay inside the JSX.

App.jsxApp.jsx
function Item({ name, isPacked }) {
  return <li>{isPacked ? name + " (packed)" : name}</li>;
}

The markup no longer repeats the list item tag. The question mark separates the condition from the true branch, and the colon separates the true branch from the false branch.

4. Logical AND operator

The AND operator renders its right side only when the left side is truthy. It is the shortest way to show something or nothing.

App.jsxApp.jsx
function Item({ name, isPacked }) {
  return <li>{name} {isPacked && "(packed)"}</li>;
}

The packed label appears only when isPacked is true. When the condition is false, React renders nothing in its place.

Do not put a raw number on the left

A zero on the left side renders as the number 0. Write count > 0 before the AND operator instead of using count alone.

5. Assign JSX to a variable

For a longer condition, store the result in a variable and render the variable.

App.jsxApp.jsx
function Item({ name, isPacked }) {
  let label = name;
 
  if (isPacked) {
    label = name + " (packed)";
  }
 
  return <li>{label}</li>;
}

The label starts as the name and changes only when needed. This is the most flexible pattern because the if block can grow without crowding the markup.

6. Return null

When a component should disappear from the screen entirely, return null.

App.jsxApp.jsx
function Item({ name, hidden }) {
  if (hidden) {
    return null;
  }
 
  return <li>{name}</li>;
}

A hidden item renders nothing, and React leaves no element in the list. In practice, filtering the data before rendering is often clearer, as the list guide explains.

Choose the right pattern

Start with an early return when one case is an exception. Reach for a ternary when both branches are short and belong in the markup. Use the AND operator to show or hide a small piece, and switch to a variable when the logic no longer fits on one line.

The braces that hold these expressions are covered in the guide on rendering variables and expressions in JSX. For hiding items inside a list, the guide on rendering lists with map shows filtering with stable keys.

Rune AI

Rune AI

Key Insights

  • Conditional rendering uses plain JavaScript, not template syntax.
  • Early returns handle the simplest case first.
  • A ternary fits a single either or choice inside JSX.
  • The AND operator renders its right side only when the left side is truthy.
  • Never put a raw number on the left side of the AND operator.
RunePowered by Rune AI

Frequently Asked Questions

What is the easiest conditional rendering pattern?

An early return with an if statement is the clearest for a beginner. Return one piece of JSX when a condition is true, then return the default JSX below it.

Why does a zero render on screen with the AND operator?

The AND operator returns its left side when it is falsy. If the left side is the number 0, React renders that 0. Compare against zero instead, such as count > 0.

Can I write an if statement inside JSX?

No. JSX braces hold expressions, not statements. Put the if statement in the function body, or use a ternary expression inside the braces.

Conclusion

Conditional rendering is just JavaScript control flow applied to JSX. Return early with if, choose between two branches with if and else, or tighten the markup with a ternary, the AND operator, a variable, or null. Pick the pattern that keeps the intent clearest.