Conditional Rendering vs Visibility Transitions
What you'll learn: When to use opacity: 0 with visibility or pointer-events instead of unmounting components for smooth exit animations.
The Core Problem
When you conditionally render a component ({show && <Component />}), React immediately unmounts it when show becomes false. The DOM element vanishes instantly—there's no time for an exit animation to play. This creates jarring experiences when you want smooth fade-outs or slide-outs.
The Solution: Keep It Mounted, Hide It Visually
Instead of unmounting, keep the component in the DOM but control its visual state with CSS. Use three properties together:
<div style={{
opacity: isVisible ? 1 : 0,
visibility: isVisible ? 'visible' : 'hidden',
pointerEvents: isVisible ? 'auto' : 'none',
transition: 'opacity 300ms'
}}>
{/* Content always mounted */}
</div>
Why each property matters:
opacity: 0— Makes it invisible but still takes up layout spacevisibility: hidden— Removes it from accessibility tree and prevents focuspointer-events: none— Disables all mouse/touch interactionstransition— Animates the opacity change smoothly
When to Use Each Approach
Use conditional rendering when:
- Component is expensive to keep mounted (large lists, complex calculations)
- No exit animation needed
- Component won't be shown/hidden frequently
Use visibility transitions when:
- Exit animations are essential to UX (modals, tooltips, dropdowns)
- Toggle happens frequently (hover states, accordions)
- Component state should persist while hidden
Key Takeaway: For exit animations, keep components mounted and use opacity + visibility + pointer-events instead of unmounting. React can't animate something that no longer exists in the DOM.