Course topics

By WebNest Studio Engineering Team

React Tutorial

Conditional Rendering in React

Conditional rendering means showing different UI depending on some condition — a loading state, whether a user is logged in, whether a list is empty. Because JSX is just JavaScript, you don't need a special templating syntax for this; you use ordinary JavaScript conditional constructs directly inside or around your JSX.

The two most common patterns are the ternary operator for choosing between two alternatives, and the logical AND (&&) operator for rendering something only when a condition is true, with nothing rendered otherwise.

Why it matters

Interfaces change with their data: a spinner while loading, an error message on failure, a different menu for a signed-in user. In React this is ordinary JavaScript inside the component, and knowing the three or four standard patterns, and the one trap with &&, covers every case.

Ternary Operator for Either/Or Rendering

When you need to render one of exactly two possible outputs, the ternary operator condition ? <ThenThis /> : <ElseThis /> fits neatly inside curly braces because, unlike an if/else statement, a ternary is a single expression that evaluates to a value.

Logical AND for Optional Rendering

When there is no "else" case — you either show something or show nothing — the && operator is more concise: {isLoggedIn && <WelcomeBanner />} renders the banner only when isLoggedIn is truthy. Because && returns its left operand when that operand is falsy, care is needed with numeric values: {count && <List />} would render the literal number 0 to the page when count is 0, since 0 is falsy but not undefined.

Early Returns and Variable-Based Rendering

For more complex logic, it is often clearer to compute the JSX to render into a variable before the return statement, or to return early from the component function entirely for special cases like a loading or error state, keeping the main return statement focused on the "happy path" UI.

  • Use a ternary for exactly two branches directly inside JSX.
  • Use && for "render this, or render nothing" cases, guarding against falsy-but-not-boolean values like 0 or an empty string.
  • Use an early return at the top of the component for entirely different UI states, such as a loading spinner or an error message.

Example

Combining an early return, a ternary, and && in one component

JavaScript
function OrderStatus({ isLoading, isPaid, itemCount }) {
  if (isLoading) {
    return <p>Loading order...</p>;
  }

  return (
    <div>
      <p>{isPaid ? 'Payment received' : 'Payment pending'}</p>
      {itemCount > 0 && <p>{itemCount} item(s) in this order.</p>}
    </div>
  );
}

export default function App() {
  return <OrderStatus isLoading={false} isPaid={true} itemCount={3} />;
}
Output
Because isLoading is false, the early-return loading message is skipped; the ternary renders "Payment received" (isPaid is true); and since itemCount (3) is greater than 0, "3 item(s) in this order." is also rendered below it.

Common Mistakes

  • Using {count && <List items={items} />} when count can be 0, which renders a stray "0" on the page instead of nothing, because 0 is falsy but && returns it as-is.
  • Nesting several ternary operators inside JSX until the logic becomes unreadable — extracting the decision into a variable or a helper function is usually clearer.
  • Trying to use an if statement directly inside curly braces in JSX, which is a syntax error because if is a statement, not an expression.
  • Forgetting to handle the loading or error state at all, letting the component try to render data that has not arrived yet and crash.

Exercise

Do this in src/App.jsx of a Vite + React project. Write a Status component with three props. If loading is true it returns a paragraph reading Loading. Otherwise it shows either "Welcome back" or "Please sign in", depending on isLoggedIn, and, only when unread is greater than zero, a line with the number of new messages.

Expected result: with loading false, isLoggedIn true and unread 3, the page shows "Welcome back" and "3 new messages"; with unread 0, the second line is absent and no stray 0 is shown.

JavaScript · starter code
function Status({ loading, isLoggedIn, unread }) {
  // TODO: early return while loading

  return (
    <div>
      {/* TODO: one of two messages, depending on isLoggedIn */}
      {/* TODO: the unread line, only when unread > 0 */}
    </div>
  );
}

export default function App() {
  return <Status loading={false} isLoggedIn={true} unread={3} />;
}
Show hints
  • An early return handles the loading case before the main JSX.
  • Write the condition as a real boolean: {unread > 0 && ...}. {unread && ...} would render 0.
Show solution
JavaScript · solution
function Status({ loading, isLoggedIn, unread }) {
  if (loading) {
    return <p>Loading</p>;
  }

  return (
    <div>
      <p>{isLoggedIn ? "Welcome back" : "Please sign in"}</p>
      {unread > 0 && <p>{unread} new messages</p>}
    </div>
  );
}

export default function App() {
  return <Status loading={false} isLoggedIn={true} unread={3} />;
}

Quick quiz

1. What does {count && <p>Items</p>} render when count is 0?

  • A. Nothing
  • B. The paragraph
  • C. The number 0
  • D. An error
Show answer

C. The expression evaluates to 0, and React renders numbers. Use count > 0 as the condition.

2. Which values does React render as nothing?

  • A. false, null and undefined
  • B. 0 and an empty string only
  • C. Every falsy value
  • D. Only null
Show answer

A. true is also not rendered. The number 0 is rendered.

3. Can an if statement be written directly inside JSX braces?

  • A. Yes
  • B. Only in class components
  • C. Only with a key
  • D. No; braces take expressions, so a ternary or && is used, or the if goes before the return
Show answer

D. An if is a statement, and JSX braces accept only expressions.

Interview questions

What are the ways to render conditionally in React?

Show answer

An early return before the main JSX, for cases such as loading or error. The conditional operator, condition ? a : b, to choose between two outputs. The logical AND, condition && element, to show something or nothing. Assigning JSX to a variable with ordinary if statements and rendering the variable. Returning null renders nothing at all.

What is the difference between rendering nothing and hiding with CSS?

Show answer

When a component is not rendered, it is removed from the tree: its DOM nodes are gone and its state is discarded, so it starts fresh next time. When it is hidden with CSS, such as display: none, it stays mounted: its state and DOM are kept and it still takes part in rendering. Removing is the default; hiding is used when state such as scroll position or form input must survive.

Key Points to Remember

  • Conditional rendering uses ordinary JavaScript expressions (ternary, &&) inside JSX, not a separate templating syntax.
  • The ternary operator condition ? a : b is the standard tool for choosing between two rendering options.
  • The && operator renders its right-hand side only when the left-hand side is truthy — watch out for falsy numbers like 0.
  • Early returns at the top of a component keep the main JSX focused on the primary rendering case.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of plain HTML, CSS and JavaScript. JSX needs a React project on your computer. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering