diff --git a/ReactCommon/fabric/components/root/RootShadowNode.cpp b/ReactCommon/fabric/components/root/RootShadowNode.cpp index df4a400cff..7005841258 100644 --- a/ReactCommon/fabric/components/root/RootShadowNode.cpp +++ b/ReactCommon/fabric/components/root/RootShadowNode.cpp @@ -44,38 +44,35 @@ UnsharedRootShadowNode RootShadowNode::clone( } UnsharedRootShadowNode RootShadowNode::clone( - const SharedShadowNode &oldShadowNode, - const SharedShadowNode &newShadowNode) const { - std::vector> ancestors; + SharedShadowNode const &oldShadowNode, + SharedShadowNode const &newShadowNode) const { + auto ancestors = oldShadowNode->getAncestors(*this); - if (!oldShadowNode->constructAncestorPath(*this, ancestors)) { + if (ancestors.size() == 0) { return UnsharedRootShadowNode{nullptr}; } - auto oldChild = oldShadowNode; - auto newChild = newShadowNode; + auto childNode = newShadowNode; - for (const auto &ancestor : ancestors) { - auto oldParent = ancestor.get().shared_from_this(); + for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) { + auto &parentNode = it->first.get(); + auto childIndex = it->second; - auto children = oldParent->getChildren(); - std::replace(children.begin(), children.end(), oldChild, newChild); + auto children = parentNode.getChildren(); + assert(ShadowNode::sameFamily(*children.at(childIndex), *childNode)); + children[childIndex] = childNode; - auto sharedChildren = std::make_shared(children); - auto newParent = oldParent->clone({ - /* .tag = */ ShadowNodeFragment::tagPlaceholder(), - /* .rootTag = */ ShadowNodeFragment::surfaceIdPlaceholder(), - /* .props = */ ShadowNodeFragment::propsPlaceholder(), - /* .eventEmitter = */ ShadowNodeFragment::eventEmitterPlaceholder(), - /* .children = */ sharedChildren, + childNode = parentNode.clone({ + ShadowNodeFragment::tagPlaceholder(), + ShadowNodeFragment::surfaceIdPlaceholder(), + ShadowNodeFragment::propsPlaceholder(), + ShadowNodeFragment::eventEmitterPlaceholder(), + std::make_shared(children), }); - - oldChild = oldParent; - newChild = newParent; } return std::const_pointer_cast( - std::static_pointer_cast(newChild)); + std::static_pointer_cast(childNode)); } } // namespace react