Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Props and Attributes

React animation and transition events

Reference for React animation and transition events: onAnimationStart, onAnimationEnd, onAnimationIteration and onTransitionEnd, with propertyName and bubbling.

CSS animations and transitions run in the browser, not in React, but React lets you listen to their milestones. onTransitionEnd fires when a CSS transition finishes. onAnimationStart, onAnimationIteration and onAnimationEnd mark the start, each repeat and the end of a @keyframes animation.

Typical uses: remove an element from the DOM after its exit animation, focus a field once a panel has slid open, or count how many times a loading animation has repeated. The events bubble, so a handler on a parent also sees events from its children.

JSX
<div
  className="panel"
  onTransitionEnd={(e) => console.log(e.propertyName)}
/>

Try it

Know when a transition finished
Count animation iterations

Syntax

JSX
<element
  onTransitionEnd={(event: React.TransitionEvent) => void}
  onAnimationStart={(event: React.AnimationEvent) => void}
  onAnimationIteration={(event: React.AnimationEvent) => void}
  onAnimationEnd={(event: React.AnimationEvent) => void}
/>
Name Description
event.propertyName (transition) The CSS property that finished, such as "width" or "opacity".
event.elapsedTime Seconds the transition or animation ran, not counting delay.
event.pseudoElement "" for the element itself, or "::before" and similar.
event.animationName (animation) The name from @keyframes.
event.target / event.currentTarget The element the animation ran on / the element holding your handler.

Edge cases: the events never fire if the style does not actually change (same value, display: none, or transition: none), if the transition is cancelled by another change, or if the user has set reduced motion and your CSS removes the animation. Never make required logic depend on them alone.

Usage notes

  • One event per property. A rule such as transition: all 0.3s that changes width and color fires transitionend twice. Check propertyName to react to one of them.
  • They bubble. A child's animation triggers the parent's handler. Compare e.target === e.currentTarget when you only want the parent's own.
  • Cancelled transitions send transitioncancel, not transitionend. The matching onTransitionCancel prop exists, along with onTransitionRun and onTransitionStart in current React versions.
  • Animation forwards and infinite. animationend never fires for an infinite animation. animationiteration fires at the end of each loop except the last one.
  • Exit animations. To animate before unmounting, keep the element rendered with an "exiting" class, then drop it in onAnimationEnd. Add a fallback timeout in case the event does not arrive.
  • Respect prefers-reduced-motion. Provide a CSS rule that disables the motion, and do not rely on its events for logic.
  • Do not animate in JavaScript state. Updating state on every frame is slow. Let CSS animate and use these events only for milestones.
  • Testing. jsdom does not run CSS animations, so tests dispatch transitionend or animationend manually.