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"}', ]); });