React Conditional Rendering

Learn to control your UI with logic. Master ternaries, `&&`, and more to show the right component at the right time.

Lesson ProgressStep 1 of 9
Dashboard
AdminPanel
0 EXP

Welcome! Let's see how React decides *what* to show. This is conditional rendering.

// We'll render based on a condition

The Ternary Operator (`? :`)

The ternary operator is the most common way to handle a simple if-else condition directly inside your JSX. It's compact and powerful.

The syntax is: condition ? expressionIfTrue : expressionIfFalse

return (
  <div>
    {isLoggedIn ? <UserDashboard /> : <LoginForm />}
  </div>
);

System Check

In a ternary operator, what character separates the 'true' condition from the 'false' condition?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Ternary Master

Use a ternary operator for conditional rendering.

🏗️
&& Whiz

Correctly use the logical AND operator for rendering.

✍️
Null Render Expert

Properly return null to render nothing.

Mission: Build a Greeting

Complete the `UserGreeting` component. It receives an `isLoggedIn` prop. If true, return `

Welcome back!

`. If false, return `

Please log in.

`.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Component Logic

Drag the lines of code into the correct order to create a valid `useState` implementation.

const [count, setCount] = useState(0);
import { useState } from 'react';
return <button onClick={() => setCount(count + 1)}>

Challenge: Complete the Syntax

Fill in the missing parts to conditionally render a 'Loading...' message only if `isLoading` is true.

{ <>Loading...</div> }

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "User Greeting" component for feedback from other Net-Runners.

React's Dynamic UI: A Deep Dive into Conditional Rendering

In React, your components are blueprints. They describe *what* the UI should look like based on the current **state** and **props**. **Conditional rendering** is the term for the techniques you use to return different elements based on those inputs. This is how you create a UI that feels alive and responsive, showing loaders, error messages, user dashboards, or login forms.

Method 1: The Ternary Operator (If-Else)

This is the most common and direct way to handle an "if-else" scenario directly within your JSX. The syntax is condition ? expressionIfTrue : expressionIfFalse. It's perfect for when you must render one of two things.

function UserGreeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn 
        ? <h1>Welcome back!</h1> 
        : <h1>Please sign up.</h1>
      }
    </div>
  );
}

Method 2: The Logical && Operator (If)

What if you only want to render something *if* a condition is true, and render *nothing* otherwise? The logical `&&` operator is your best friend. In JavaScript, true && expression always evaluates to `expression`, and false && expression evaluates to `false`. React knows not to render `false`, so it works perfectly.

function Mailbox({ unreadMessages }) {
  return (
    <div>
      {unreadMessages.length > 0 &&
        <h2>
          You have {unreadMessages.length} unread messages.
        </h2>
      }
    </div>
  );
}
Warning: Be careful with `&&`. If the left-hand side is a **number** like `0`, the expression will evaluate to `0` and React **will render the number 0** to the screen, which is usually a bug. Always ensure the left side is a true boolean (e.g., unreadMessages.length > 0).

Method 3: `if` Statements & Element Variables

Sometimes your logic is too complex for a ternary or `&&`. You can't put a plain `if` statement inside JSX, but you can use one *before* the `return` statement to prepare your UI in a variable. This is often the cleanest and most readable approach for complex components.

function LoginButton({ status }) {
  let buttonContent;
  let buttonColor;

  if (status === 'loading') {
    buttonContent = 'Loading...';
    buttonColor = 'gray';
  } else if (status === 'error') {
    buttonContent = 'Try Again';
    buttonColor = 'red';
  } else {
    buttonContent = 'Login';
    buttonColor = 'blue';
  }

  return <button className={buttonColor}>{buttonContent}</button>;
}

Method 4: Returning `null` (Preventing Render)

In some cases, you don't want a component to render *anything* at all. You can explicitly return `null` from a component to tell React to render nothing. This is different from returning an empty `<div>`—`null` renders literally nothing.

function AdminPanel({ isAdmin }) {
  if (!isAdmin) {
    return null;
  }

  return (
    <div>
      {/* ... super secret admin stuff ... */}
    </div>
  );
}
Key Takeaway: Choose the right tool for the job. Use a **ternary** for simple if-else, `&&` for "if-and-nothing-else", and **element variables** with `if` statements for complex logic. Returning `null` is a powerful tool to prevent rendering entirely.

React Conditional Rendering Glossary

Conditional Rendering
The practice of outputting different JSX elements or components based on the current state, props, or other logic within a React component.
Ternary Operator (`? :`)
A JavaScript operator that provides a compact syntax for an if-else statement. It is the most common way to write an `if-else` condition directly inside JSX.
Logical AND Operator (`&&`)
A JavaScript operator used in React to render an element *only if* a condition is true. If the condition is false, it "short-circuits" and renders nothing (or the falsey value).
`null`
A valid value to return from a React component's `render` method (or function body). Returning `null` tells React to render nothing for that component, without throwing an error.
Short-Circuiting
The behavior of logical operators (`&&`, `||`) where the second operand is only evaluated if the first operand doesn't suffice to determine the result. React uses this (`condition && <Element/>`) to avoid rendering.
Element Variable
A variable (e.g., `let content;`) declared inside a component's body, often used with `if/else` or `switch` statements to store the JSX that will be rendered *before* the final `return` statement.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching React and building robust, dynamic web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest React specifications (Hooks) and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!