Skip to content

Commit eafe1f9

Browse files
fix(runtime): append newChild if parent node doesn't match with patched node (#6141)
1 parent 4add75c commit eafe1f9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/runtime/dom-extras.ts

+15
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ const patchInsertBefore = (HostElementPrototype: HTMLElement) => {
299299
});
300300
if (found) return newChild;
301301
}
302+
303+
/**
304+
* Fixes an issue where slotted elements are dynamically relocated in React, such as after data fetch.
305+
*
306+
* When a slotted element is passed to another scoped component (e.g., <A><C slot="header"/></A>),
307+
* the child’s __parentNode (original parent node property) does not match this.
308+
*
309+
* To prevent errors, this checks if the current child's parent node differs from this.
310+
* If so, appendChild(newChild) is called to ensure the child is correctly inserted,
311+
* allowing Stencil to properly manage the slot placement.
312+
*/
313+
const parentNode = (currentChild as d.PatchedSlotNode)?.__parentNode;
314+
if (parentNode && !this.isSameNode(parentNode)) {
315+
return this.appendChild(newChild);
316+
}
302317
return (this as d.RenderNode).__insertBefore(newChild, currentChild);
303318
};
304319
};

0 commit comments

Comments
 (0)