Limitations: No Mount/Unmount Animations
You'll learn why CSS transitions alone can't animate components appearing or disappearing from the DOM.
The Core Problem
CSS transitions work beautifully when a component stays mounted and just changes classes or styles. But when React adds or removes a component from the DOM entirely, there's no transition phase—the element instantly appears or vanishes.
Think of it like a light switch: CSS transitions can dim a light smoothly when it's already on, but they can't animate the bulb being screwed in or unscrewed from the socket. The element must exist in the DOM for the browser to transition its styles.
Why This Happens
When you conditionally render with {isOpen && <Modal />}, React either mounts the component (adds it to DOM) or unmounts it (removes it completely). The browser has nothing to transition from or to because the element doesn't exist before mounting or after unmounting.
// ❌ No exit animation happens
{showModal && <Modal className="fade-in" />}
// Modal just disappears when showModal becomes false
The Manual Workaround
To animate unmounting, you need to:
- Keep the component mounted even when "closing"
- Trigger the exit animation with a class
- Wait for
onTransitionEndto fire - Then actually unmount it
This requires managing timing state manually or using animation libraries like Framer Motion or React Transition Group that handle this delay logic for you.
Key Takeaway: CSS transitions require elements to exist in the DOM—they can't animate mounting or unmounting without manual delays or specialized libraries to defer removal until animations complete.