From 670d7695775a81efabe8f9d62966872053ae65ab Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Mon, 16 Mar 2026 09:32:16 -0700 Subject: [PATCH] Inline onTransitionAnimationEnd and fix iterator invalidation in ViewTransitionModule (#56104) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56104 `startViewTransitionEnd` was removing entries from `nameRegistry_` while iterating over it via `onTransitionAnimationEnd`, which is undefined behavior. This diff improves the fix by: - Inlining `onTransitionAnimationEnd` (private, single call site) - Clearing `nameRegistry_` after the loop instead of erasing during iteration, removing the need for a copy ## Changelog: [Internal] [Fixed] - Inline onTransitionAnimationEnd and fix iterator invalidation in ViewTransitionModule Reviewed By: christophpurrer Differential Revision: D96085815 fbshipit-source-id: b8e425d1166421cd10b27ea5d366d9fdef58d468 --- .../viewtransition/ViewTransitionModule.cpp | 25 +++++-------------- .../viewtransition/ViewTransitionModule.h | 2 -- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp index d0b014a563f..94bf7dd6dd5 100644 --- a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp +++ b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp @@ -131,9 +131,13 @@ void ViewTransitionModule::startViewTransition( } void ViewTransitionModule::startViewTransitionEnd() { - for (const auto& it : nameRegistry_) { - onTransitionAnimationEnd(it.second, it.first, 0); + for (const auto& [tag, names] : nameRegistry_) { + for (const auto& name : names) { + oldLayout_.erase(name); + newLayout_.erase(name); + } } + nameRegistry_.clear(); transitionStarted_ = false; } @@ -170,21 +174,4 @@ ViewTransitionModule::getViewTransitionInstance( return std::nullopt; } -void ViewTransitionModule::onTransitionAnimationEnd( - const std::unordered_set& names, - Tag newTag, - Tag oldTag) { - for (const auto& name : names) { - oldLayout_.erase(name); - newLayout_.erase(name); - } - - if (newTag != 0) { - nameRegistry_.erase(newTag); - } - if (oldTag != 0) { - nameRegistry_.erase(oldTag); - } -} - } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h index c9d617f7f17..45e2ab45c67 100644 --- a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h +++ b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h @@ -62,8 +62,6 @@ class ViewTransitionModule : public UIManagerViewTransitionDelegate { }; private: - void onTransitionAnimationEnd(const std::unordered_set &names, Tag newTag, Tag oldTag); - // registry of layout of old/new views std::unordered_map oldLayout_{}; std::unordered_map newLayout_{};