React · Conditional Rendering
React lets you use JavaScript’s logical && (AND) operator for short-circuit rendering. It’s a concise way to show an element only when a condition is true.
condition && element → React renders the element only if the condition is truthy.0 or other falsy values unless you understand the behavior.a && b returns b if a is truthy, else returns a.false, 0, "", null, undefined, and NaN are falsy.&& for simple one-way conditions. Use a ternary for “either A or B”.General pattern in JSX:
{condition && <SomeComponent />}{isLoggedIn && <p>Welcome back!</p>}{items.length === 0 && <p>No items yet</p>}If condition is:
false, 0, null etc., and renders nothing for that expression.In React JSX, condition && element means: “Render the element only if the condition is true.”
// Show a special message only when the user is logged in
function WelcomeMessage({ isLoggedIn }) {
return (
<div>
<h4>Hello Visitor!</h4>
{isLoggedIn && <p className="text-success">Welcome back, user! ?</p>}
</div>
);
}
isLoggedIn = true → “Hello Visitor!” + “Welcome back, user! ?” are both shown.isLoggedIn = false → Only “Hello Visitor!” is shown (no extra paragraph).Use short-circuit rendering to show or hide notification text based on the number of unread messages:
// Show inbox notification only when there is at least one new message
function Notifications({ count }) {
return (
<div className="p-2">
<h5>Inbox</h5>
{count > 0 && <p className="text-info">You have {count} new messages! ✉️</p>}
</div>
);
}
count = 3 → Inbox + “You have 3 new messages! ✉️”.count = 0 → Only the “Inbox” heading, no extra paragraph.You can chain multiple && expressions for more detailed access control or display logic:
// Show admin button only when the user is both logged in and an admin
function AccessPanel({ isLoggedIn, isAdmin }) {
return (
<div>
{isLoggedIn && <p>Welcome User</p>}
{isLoggedIn && isAdmin && (
<button className="btn btn-danger">Admin Settings</button>
)}
</div>
);
}
isLoggedIn = false → Nothing is shown.isLoggedIn = true, isAdmin = false → “Welcome User”.isLoggedIn = true, isAdmin = true → “Welcome User” + “Admin Settings” button.Short-circuit rendering is handy for showing an “empty state” when lists have no items:
// Show a 'No tasks' message when the todo list is empty
function TodoList({ todos }) {
return (
<div>
<h5>My Tasks</h5>
{todos.length === 0 && (
<p className="text-muted">No tasks available ?</p>
)}
<ul>
{todos.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
);
}
todos = [] → “My Tasks” + “No tasks available ?”, no list items.todos = ["Buy milk", "Write code"] → “My Tasks” + the two list items; “No tasks” message is not shown.JavaScript treats some values as falsy — e.g. 0, "", null, undefined, and false. If you use them on the left of &&, React will not render the element.
// ⚠️ Problem: 0 is falsy, so nothing is shown when the cart is empty
function Cart({ items }) {
return (
<p>Total items: {items.length && items.length}</p>
);
}
// ✅ Fix: use a ternary operator so 0 is still displayed
function CartFixed({ items }) {
return (
<p>Total items: {items.length > 0 ? items.length : 0}</p>
);
}
You can combine short-circuit rendering with other techniques (like ternaries) to keep your JSX clean and expressive:
// Render different messages based on whether a user object is present
function Dashboard({ user }) {
return (
<div>
<h4>Dashboard</h4>
{user && <p>Welcome, {user.name}!</p>}
{!user && (
<p className="text-danger">Please log in to access your data.</p>
)}
</div>
);
}
user = null → Only the “Please log in…” message is rendered.user = {`{ name: "Sam" }`} → “Welcome, Sam!” is rendered; error message is hidden.&& for simple, one-directional conditions (show or hide).&& if you need “either A or B” — use a ternary instead.0; use a ternary to display numeric values safely.unreadCount > 0.isAdmin is true.0, "", null) on the left of && and observe the behavior.Goal: Use JavaScript’s && operator to simplify React conditional rendering, and confidently decide when to use && versus the ternary operator.