0Pricing
React Academy · Lesson

Enter and Exit Animations Without a Library

Implement mount/unmount animations by controlling display and opacity timing manually in React.

Enter and Exit Animations Without a Library is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Unmounting Problem

When you conditionally render with {show && }, React removes the element from the DOM the instant show becomes false. There is no time for an exit animation because the node is already gone.

Enter animations are easy; exit animations are the real challenge.

Solution 1: Keep It Mounted

The simplest fix is to never unmount. Keep the element rendered and animate opacity to zero plus pointer-events: none when hidden, and back to one when shown.

This gives smooth fades in both directions, at the cost of the element staying in the DOM.

Solution 2: A Mounting State Machine

For true unmounting you model a small state machine with phases like mounting, mounted, unmounting, and unmounted. You only remove the node once the exit animation has finished.

State drives which animation classes apply at each phase.

A useMount Hook

You can encapsulate the machine in a custom hook, often called useMount. It takes the desired visibility and returns whether the element should be in the DOM plus its current phase.

Components stay clean because the timing logic lives in the reusable hook.

Enter and Exit Class Pairs

Borrowing from common conventions, you apply enter then enter-active on mount, and exit then exit-active on removal. The base class sets the start state and the active class sets the end state with a transition.

Toggling between the two on the right frame produces the animation.

Listening for transitionend

To know when an exit animation finishes, listen for the transitionend event on the element. When it fires for the exit, you flip the state to unmounted and remove the node.

This ties the unmount precisely to the end of the visual animation rather than a guessed timeout.

A Reusable Transition Component

You can wrap all of this in a Transition component that accepts an in prop and children. It keeps children mounted through their exit animation and applies the proper classes per phase.

Consumers just toggle in and get correct enter and exit motion for free.

Triggering the Active Class Correctly

A subtle detail: you must let the browser paint the start class before adding the active class, otherwise it jumps with no transition. A requestAnimationFrame or forced reflow between the two ensures the transition actually runs.

This guarantees the browser registers the start values first.

Comparing to react-transition-group

The library react-transition-group provides Transition and CSSTransition components that implement exactly this pattern, including the class lifecycle and unmount timing.

Your DIY version mirrors its core ideas, which makes the library easy to understand once you have built one yourself.

When to Use a Library vs DIY

For a single simple fade, the keep-mounted approach or a small hook is plenty and avoids a dependency. For many coordinated transitions, lists with add and remove, or complex sequencing, a library saves time and edge-case bugs.

Choose based on how much animation complexity your app actually needs.

Putting It Together

Exit animations require keeping the element alive until its animation completes, whether by never unmounting, by a state machine plus transitionend, or by a library.

Understanding the mechanics lets you pick the lightest approach that meets your needs.

Quick Check

Test your understanding of exit animations.

Recap

You learned why exit animations are hard, two solutions (keep-mounted and a mounting state machine), how transitionend times the unmount, and how to build a reusable Transition component.

You also saw when to reach for react-transition-group instead of DIY.

Frequently asked questions

Is the “Enter and Exit Animations Without a Library” lesson free?

Yes — the full text of “Enter and Exit Animations Without a Library” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enter and Exit Animations Without a Library”?

Implement mount/unmount animations by controlling display and opacity timing manually in React. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enter and Exit Animations Without a Library” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CSS Transitions with React State Changes
  2. CSS Keyframe Animations with React
  3. useLayoutEffect for Pre-Animation Measurements
  4. Enter and Exit Animations Without a Library
← Back to React Academy