Conditional Rendering
Showing and hiding UI with if statements, ternary operators, and logical AND operators.
What you'll learn
- Using ternary operators for if-else rendering
- Using logical AND (&&) for show-or-hide rendering
- Extracting conditions into variables for readability
- Returning null to render nothing
Concept
Rendering Based on Conditions
In React, conditional rendering means showing different UI based on the current state or props. Because JSX is JavaScript, you can use standard JavaScript conditional patterns directly in your JSX. There is no special template directive like *ngIf in Angular or v-if in Vue — you use plain JavaScript.
The Ternary Operator
The ternary operator condition ? true : false is the most common pattern for if-else rendering in JSX. For example, {isLoggedIn ? <Dashboard /> : <Login />} shows the dashboard for logged-in users and the login form otherwise. The ternary is an expression (it returns a value), which is why it works inside JSX curly braces. Keep ternaries shallow — deeply nested ternaries are hard to read.
Logical AND (&&)
The logical AND operator && is useful for show-or-hide rendering where there is no else branch. {hasError && <ErrorMessage />} renders the error message only when hasError is true. Be careful: if the left side is 0 or an empty string, React will render that value. Always ensure the left side is a boolean, or use {!!value && <Component />} to coerce to boolean.
Returning null
Sometimes the cleanest approach is to return null from a component when it should not render anything. This is useful when the condition is complex or when a component should entirely hide itself. You can also extract conditions into variables above the return statement for readability: const showHeader = isLoggedIn && hasProfile; then {showHeader && <Header />}.
Element Variables
You can store JSX elements in variables and conditionally include them. This is useful when the branches are complex: let button = <LoginButton />; if (isLoggedIn) button = <LogoutButton />; then use {button} in your JSX. This keeps the return statement clean and the logic readable. For more complex conditions, consider extracting the conditional rendering into a separate sub-component that encapsulates the logic.
Example
import { useState } from "react";
function UserGreeting({ user }) {
// Extract conditions into variables for readability.
const showAdminPanel = user?.role === "admin";
const hasUnreadMessages = user?.unreadCount > 0;
return (
<div>
{/* Ternary for if-else rendering */}
{user ? (
<h1>Welcome back, {user.name}!</h1>
) : (
<h1>Please sign in.</h1>
)}
{/* Logical AND for show-or-hide */}
{showAdminPanel && <AdminPanel />}
{/* Coerce to boolean to avoid rendering 0 */}
{!!hasUnreadMessages && (
<p>You have {user.unreadCount} unread messages.</p>
)}
</div>
);
}
Three patterns: ternary for if-else, && for show-or-hide, and !! to coerce to boolean. Extracting conditions into variables keeps JSX readable.
Try it
- JSON Formatter
Inspect the data that drives your conditional rendering logic.
- Code Minifier
See how conditional rendering compiles and minifies.
Common mistakes
The mistake
Using && with a number that renders 0 on screen
The fix
If the left side of && is 0, React renders '0'. Coerce to boolean with !!value or use a ternary: value > 0 ? <Component /> : null.
The mistake
Nesting ternary operators too deeply
The fix
Deeply nested ternaries are unreadable. Extract to a variable, use a helper function, or a lookup object/map. Keep JSX expressions flat and clear.
Practice Conditional Rendering
8 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.