To conditionally show or hide components and elements in React, evaluate a boolean or state condition directly inside JSX using (logical AND) or a ternary operator (condition ? <A /> : <B />) to mount or unmount elements, or toggle CSS visibility/display styles (such as display: none or a class name) when you want to keep an element mounted in the DOM and preserve its internal state or scroll position. Choosing between conditional rendering (unmounting) and CSS hiding (staying mounted) depends on whether you want child effects/state to reset or stay alive.
1. Conditional Rendering with Logical and Ternary Operators
When a component or element should only exist in the DOM under a specific condition, render it conditionally inside JSX with or a ternary expression:
// Render or omit elements conditionally in React JSX
function AccountBanner({ isLoggedIn, unreadCount }) {
return (
<section>
{isLoggedIn ? (
<p>Welcome back to your workspace.</p>
) : (
<p>Please sign in to continue.</p>
)}
{unreadCount > 0 && (
<span className="badge">You have {unreadCount} unread messages</span>
)}
</section>
);
}
- Avoid numeric
0rendering: Use an explicit boolean check likeunreadCount > 0rather thanunreadCount, because React renders a falsy number0into the DOM instead of nothing. - Unmounting resets state: When a condition becomes false and removes a component, React unmounts it and cleans up its state and effects until it mounts again.
2. Preserving Mounted State with CSS Hiding (display or Classes)
When toggling a panel, drawer, or form frequently—and you do not want to lose local state, form inputs, or expensive setup on each toggle—keep the element mounted and hide it via CSS:
// Keep the element mounted in the DOM while toggling visibility via style/class
function CollapsiblePanel({ isOpen, children }) {
return (
<div
className={isOpen ? "panel panel--open" : "panel panel--closed"}
style={{ display: isOpen ? "block" : "none" }}
aria-hidden={!isOpen}
>
{children}
</div>
);
}
- Preserves state and DOM nodes: Keeping the element mounted retains child
useState, input values, and DOM layout state across toggles. - Effects stay mounted: Unlike conditional unmounting, CSS hiding does not re-run mount/unmount
useEffectcleanup cycles when visibility changes.
3. Extracting Conditional Logic Cleanly Before return
When visibility rules involve multiple flags or roles, compute clear boolean variables or return early before the JSX return statement to keep markup readable:
// Compute clear visibility flags before returning JSX
function ActionToolbar({ user, isMaintenanceMode, onEdit }) {
if (isMaintenanceMode) {
return <p className="notice">Editing is temporarily paused.</p>;
}
const canEdit = Boolean(user && (user.role === "admin" || user.role === "editor"));
return (
<div className="toolbar">
{canEdit && (
<button type="button" onClick={onEdit}>
Edit Document
</button>
)}
</div>
);
}
- Early returns simplify branches: Returning early for blocking states (like loading, error, or maintenance) reduces deeply nested ternaries in JSX.
- Use
nullfor “render nothing”: Returningnullfrom a component (or renderingnullin a ternary branch) renders nothing in React without throwing an error.
TL;DR: Use condition ? <Show /> : <Hide /> or booleanCondition in JSX to mount/unmount elements conditionally (guarding numeric counts with comparisons like count > 0), or toggle CSS display: none / classes when keeping the element mounted and preserving its state matters most.
