From c9a68db5c8d6843b6e4fb2f10987fba58395f46d Mon Sep 17 00:00:00 2001 From: otomad Date: Fri, 19 Apr 2024 22:34:00 +0800 Subject: [PATCH] fix: perform sync update when exiting animation https://github.com/reactjs/react-transition-group/pull/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 #816. This change makes sure that the updates are performed synchronously, in order to make sure that events fire consistently. --- src/Transition.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Transition.tsx b/src/Transition.tsx index 4c8a972e..f89a9f56 100644 --- a/src/Transition.tsx +++ b/src/Transition.tsx @@ -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"; @@ -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); + }); }); }); });