From c8d498d62d73ea8d58a6017bd0283e43d78d2c8a Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Mon, 16 Feb 2026 01:32:50 -0800 Subject: [PATCH] Add a synchronization mechanism for the layout information kept in the root nodes on the React branch (#54836) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54836 Changelog: [General][Added] Added a synchronization mechanism for the layout information between the React branch and the main branch of the ShadowTree Since layout constraints and layout context are being kept in the props of the `RootShadowNode`, it was possible for the JS revision merge to lose an update if it was being commited concurrently. Reviewed By: rubennorte Differential Revision: D88839901 fbshipit-source-id: f81d96dacf60b4e5da29da32ada468c967a2c9df --- .../react/renderer/mounting/ShadowTree.cpp | 38 +++++++++++++++++++ .../__tests__/ShadowTreeBranching-itest.js | 3 -- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp index ec806e3be22..9dbeee58107 100644 --- a/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -170,6 +170,37 @@ static std::shared_ptr progressState( }); } +/* + * Updates the layout context and layout metrics on the RootNode for commits + * coming from React. The RootNode is managed outside of it, and may have been + * modified concurrently with the React revision. + */ +static std::shared_ptr progressRootLayoutInfo( + const std::shared_ptr& shadowNode, + const RootShadowNode& baseShadowNode, + const ShadowTreeCommitOptions& commitOptions) { + if (commitOptions.source == ShadowTreeCommitSource::React || + commitOptions.source == ShadowTreeCommitSource::ReactRevisionMerge) { + const auto& mainBranchRootProps = baseShadowNode.getConcreteProps(); + const auto& jsBranchRootProps = shadowNode->getConcreteProps(); + + if (mainBranchRootProps.layoutConstraints == + jsBranchRootProps.layoutConstraints && + mainBranchRootProps.layoutContext == jsBranchRootProps.layoutContext) { + // The layout information kept in the JS branch is up to date. + return shadowNode; + } + + const auto clonedNode = baseShadowNode.ShadowNode::clone( + {.children = std::make_shared< + const std::vector>>( + shadowNode->getChildren())}); + return std::static_pointer_cast(clonedNode); + } + + return shadowNode; +} + ShadowTree::ShadowTree( SurfaceId surfaceId, const LayoutConstraints& layoutConstraints, @@ -330,6 +361,13 @@ CommitStatus ShadowTree::tryCommit( } } + if (ReactNativeFeatureFlags::enableFabricCommitBranching()) { + newRootShadowNode = progressRootLayoutInfo( + newRootShadowNode, + *oldRevisionForStateProgression.rootShadowNode, + commitOptions); + } + // Run commit hooks. newRootShadowNode = delegate_.shadowTreeWillCommit( *this, oldRootShadowNode, newRootShadowNode, commitOptions); diff --git a/packages/react-native/src/private/renderer/branching/__tests__/ShadowTreeBranching-itest.js b/packages/react-native/src/private/renderer/branching/__tests__/ShadowTreeBranching-itest.js index d37fa968119..c2db162e2f5 100644 --- a/packages/react-native/src/private/renderer/branching/__tests__/ShadowTreeBranching-itest.js +++ b/packages/react-native/src/private/renderer/branching/__tests__/ShadowTreeBranching-itest.js @@ -242,10 +242,7 @@ describe('ShadowTreeBranching', () => { ]); // Commit made from React - it should mount the final React tree - // NOTE: The RootView update should not be included here, fixed - // in a following diff expect(root.takeMountingManagerLogs()).toEqual([ - 'Update {type: "RootView", nativeID: (root)}', 'Update {type: "View", nativeID: "view"}', ]); });