Course contentsShow
← React
Lesson 139 of 2,3964. Conditional Rendering in JSXFree lesson

Style-Based Hiding vs Conditional Rendering

Learn when CSS `display: none` is wrong versus true conditional rendering, and the performance implications.

Style-Based Hiding vs Conditional Rendering

You'll learn: When using CSS to hide elements is the wrong approach compared to true conditional rendering, and why it matters for performance.

The Core Difference

When you want to hide something in React, you have two options:

  1. Style-based hiding: Render the element but make it invisible with CSS (display: none, visibility: hidden, or opacity: 0)
  2. Conditional rendering: Don't render the element at all using &&, ternaries, or early returns
// ❌ Style-based hiding (often wrong)
<div style={{ display: isVisible ? 'block' : 'none' }}>
  <ExpensiveComponent />
</div>

// ✅ Conditional rendering (usually better)
{isVisible && <ExpensiveComponent />}

Why the Distinction Matters

Style-based hiding keeps the component in the DOM. React still:

  • Mounts and updates the component
  • Runs all its hooks and effects
  • Allocates memory for its state
  • Processes re-renders when parent updates

Conditional rendering removes the component entirely. When false:

  • No component lifecycle runs
  • No memory allocated
  • No performance cost
  • Component unmounts completely

When Style-Based Hiding is Acceptable

Use CSS hiding only when:

  • You need smooth animations (fade in/out)
  • Preserving component state during hide/show
  • Layout shifts would disrupt UX

For everything else—especially expensive components or deeply nested trees—prefer conditional rendering.

Key Takeaway: Use conditional rendering (&&, ternaries) to actually remove components from the tree, not CSS to merely hide them. Style-based hiding keeps components mounted and incurs performance costs you probably don't need.