diff --git a/ReactCommon/fabric/components/root/RootShadowNode.cpp b/ReactCommon/fabric/components/root/RootShadowNode.cpp index 30d817bc6d2..f10e7ee3ce7 100644 --- a/ReactCommon/fabric/components/root/RootShadowNode.cpp +++ b/ReactCommon/fabric/components/root/RootShadowNode.cpp @@ -39,29 +39,32 @@ UnsharedRootShadowNode RootShadowNode::clone( const SharedShadowNode &oldShadowNode, const SharedShadowNode &newShadowNode) const { std::vector> ancestors; - oldShadowNode->constructAncestorPath(*this, ancestors); - if (ancestors.size() == 0) { + if (!oldShadowNode->constructAncestorPath(*this, ancestors)) { return UnsharedRootShadowNode{nullptr}; } auto oldChild = oldShadowNode; auto newChild = newShadowNode; - SharedShadowNodeUnsharedList sharedChildren; - for (const auto &ancestor : ancestors) { - auto children = ancestor.get().getChildren(); + auto oldParent = ancestor.get().shared_from_this(); + + auto children = oldParent->getChildren(); std::replace(children.begin(), children.end(), oldChild, newChild); - sharedChildren = std::make_shared(children); + auto sharedChildren = std::make_shared(children); + auto newParent = + oldParent->clone(ShadowNodeFragment{.children = sharedChildren}); - oldChild = ancestor.get().shared_from_this(); - newChild = oldChild->clone(ShadowNodeFragment{.children = sharedChildren}); + newParent->replaceChild(oldChild, newChild); + + oldChild = oldParent; + newChild = newParent; } - return std::make_shared( - *this, ShadowNodeFragment{.children = sharedChildren}); + return std::const_pointer_cast( + std::static_pointer_cast(newChild)); } } // namespace react