← Back to Chapters

Conditional Rendering in React

⚛️ Conditional Rendering in React (if, &&, ? :)

? Quick Overview

Conditional rendering in React means displaying elements or components based on certain conditions. It works just like regular JavaScript conditions — you decide what to show depending on state or variables.

For example, you show a “Login” button if a user is logged out, and a “Logout” button if the user is logged in.

✨ Conditional rendering = “Show this UI only when a condition is true.”

? Key Concepts

  • if statement – clear branching logic, often used before the return.
  • Logical AND (&&) – render something only when a condition is true.
  • Ternary operator ? : – quickly choose between two JSX blocks.
  • Combining conditions – mix all of these for complex UIs like dashboards.

? Using if Statements

The if statement is useful when you want very readable logic. You decide which JSX block to return based on props or state.

? View Code Example
// Conditionally return different JSX using an if...else block
function Greeting(props) {
if (props.isLoggedIn) {
return <h2>Welcome back!</h2>;
} else {
return <h2>Please sign in.</h2>;
}
}

? Explanation

  • If props.isLoggedIn is true → user sees “Welcome back!”.
  • If props.isLoggedIn is false → user sees “Please sign in.”.

? Using Logical AND (&&)

The && operator lets you render a piece of UI only when a condition is true. If the condition is false, React ignores the JSX on the right-hand side.

? View Code Example
// Show the message only when there is at least one notification
function Notifications({ count }) {
return (
<div>
<h3>Notifications</h3>
{count > 0 && <p>You have {count} new messages.</p>}
</div>
);
}

? Explanation

  • If count is 0 → nothing is rendered after the heading.
  • If count is greater than 0 → the paragraph is rendered.

? Using the Ternary Operator (? :)

The ternary operator provides a compact way to choose between two JSX results. It is often used directly inside JSX for simple conditions.

? View Code Example
// Use a ternary expression inside JSX to switch content
function UserStatus({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <h2>Welcome back!</h2> : <h2>Please log in.</h2>}
</div>
);
}

? Explanation

If isLoggedIn is true, the user sees “Welcome back!”. Otherwise, they see “Please log in.”. This is just a shorter version of if...else.

? Combining Conditions

You can mix if, &&, and the ternary operator for more complex flows, such as dashboards where only admins can see some parts.

? View Code Example
// Mix ternary and && to handle login and role-based messages
function Dashboard({ isLoggedIn, isAdmin }) {
return (
<div>
{isLoggedIn ? (
<>
<h2>Welcome User</h2>
{isAdmin && <p>You have admin access.</p>}
</>
) : (
<h2>Please login to continue.</h2>
)}
</div>
);
}

? Explanation

  • Not logged in → “Please login to continue.”
  • Logged in (not admin) → “Welcome User” only.
  • Logged in & admin → “Welcome User” + “You have admin access.”

? Rules & Best Practices

  • ✅ Avoid putting heavy business logic directly inside JSX — move it to helper functions.
  • ✅ Always return valid JSX (a single root element from each component).
  • ✅ Use ternary operators for simple two-way choices.
  • ✅ Use && when you want to conditionally render something without an “else”.
  • ⚠️ Avoid deeply nested ternaries — they make the code hard to read.

? Tips

  • Break large conditionals into smaller components (e.g., separate <AdminPanel /> component).
  • Use clear boolean names like isVisible, isLoggedIn, or hasAccess.
  • Keep JSX focused on “what to show”, not complex calculations.
  • Combine ternary and && only when it still feels readable.

? Try It Yourself

  1. Create a component called LoginControl with a boolean state isLoggedIn.
  2. Show “Welcome back!” if logged in, otherwise show “Please log in.”
  3. Display a button that toggles between “Login” and “Logout”.
  4. Use an if statement in the component logic and a ? : inside JSX for text changes.

Goal: Practice using if, &&, and ternary operators to render different UI based on React state or props.