Skip to content

Commit

Permalink
fix: perform sync update when exiting animation
Browse files Browse the repository at this point in the history
reactjs#885

When running in React 18 concurrent mode some state updates are batched, which results in inconsistent timing of events compared to the legacy mode. For example when using animations, after animationend event fires, the onExited event is not fired immediately, so there is a brief period of time when animation is finished and the styles are reset back to normal, which may cause a flash or a jump. One of these scenarios is described in reactjs#816.

This change makes sure that the updates are performed synchronously, in order to make sure that events fire consistently.
  • Loading branch information
otomad committed Apr 19, 2024
1 parent a426f80 commit c9a68db
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Transition.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useRef } from "react";
import type { ContextType, ReactNode } from "react";
import ReactDOM from "react-dom";
import ReactDOM, { flushSync } from "react-dom";

import config from "./config";
import TransitionGroupContext from "./TransitionGroupContext";
Expand Down Expand Up @@ -225,8 +225,10 @@ class TransitionComponent extends React.Component<
this.props.onExiting(node);

this.onTransitionEnd(timeouts.exit, () => {
this.safeSetState({ status: EXITED }, () => {
this.props.onExited(node);
flushSync(() => {
this.safeSetState({ status: EXITED }, () => {
this.props.onExited(node);
});
});
});
});
Expand Down

0 comments on commit c9a68db

Please sign in to comment.