[Debug] Teact: Assert when cached virtual element was moved within tree

This commit is contained in:
Alexander Zinchuk 2022-03-05 18:06:23 +01:00
parent 233979405a
commit eb0e304ff8
2 changed files with 9 additions and 1 deletions

View File

@ -98,6 +98,13 @@ function renderWithVirtual(
return $new; return $new;
} }
if (DEBUG && $new) {
const newTarget = getTarget($new);
if (newTarget && (!$current || newTarget !== getTarget($current))) {
throw new Error('[Teact] Cached virtual element was moved within tree');
}
}
if (!$current && $new) { if (!$current && $new) {
if (isNewComponent) { if (isNewComponent) {
$new = initComponent($new as VirtualElementComponent, $parent, index, parentEl); $new = initComponent($new as VirtualElementComponent, $parent, index, parentEl);

View File

@ -449,7 +449,8 @@ function forceUpdateComponent(componentInstance: ComponentInstance) {
export function getTarget($element: VirtualElement): Node | undefined { export function getTarget($element: VirtualElement): Node | undefined {
if (isComponentElement($element)) { if (isComponentElement($element)) {
return getTarget($element.children[0]); const componentElement = $element.children[0];
return componentElement ? getTarget(componentElement) : undefined;
} else { } else {
return $element.target; return $element.target;
} }