Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always clone VNodes before using them #3710

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,9 @@ function createVNode(type, props, key, __self, __source) {
props: normalizedProps,
key,
ref,
_children: null,
_parent: null,
_depth: 0,
_dom: null,
_nextDom: undefined,
_component: null,
_hydrating: null,
constructor: undefined,
_original: --vnodeId,
__source,
__self
__self,
constructor: undefined
};

// If a Component VNode, check for and apply defaultProps.
Expand Down
13 changes: 9 additions & 4 deletions src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export function createElement(type, props, children) {
}
}

return createVNode(type, normalizedProps, key, ref, null);
const vnode = {
type,
props: normalizedProps,
key,
ref,
constructor: undefined
};
if (options.vnode) options.vnode(vnode);
return vnode;
}

/**
Expand Down Expand Up @@ -75,9 +83,6 @@ export function createVNode(type, props, key, ref, original) {
_original: original == null ? ++vnodeId : original
};

// Only invoke the vnode hook if this was *not* a direct copy:
if (original == null && options.vnode != null) options.vnode(vnode);

return vnode;
}

Expand Down
18 changes: 9 additions & 9 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function diffChildren(
childVNode = renderResult[i];

if (childVNode == null || typeof childVNode == 'boolean') {
childVNode = newParentVNode._children[i] = null;
childVNode = null;
}
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
// or we are rendering a component (e.g. setState) copy the oldVNodes so it can have
Expand All @@ -59,36 +59,36 @@ export function diffChildren(
// eslint-disable-next-line valid-typeof
typeof childVNode == 'bigint'
) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = createVNode(
null,
childVNode,
null,
null,
childVNode
);
} else if (Array.isArray(childVNode)) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = createVNode(
Fragment,
{ children: childVNode },
null,
null,
null
);
} else if (childVNode._depth > 0) {
// VNode is already in use, clone it. This can happen in the following
// scenario:
} else {
// Always clone VNodes to ensure our own copy within the tree.
// This avoids clobbering VNodes used in multiple places:
// const reuse = <div />
// <div>{reuse}<span />{reuse}</div>
childVNode = newParentVNode._children[i] = createVNode(
childVNode = createVNode(
childVNode.type,
childVNode.props,
childVNode.key,
childVNode.ref ? childVNode.ref : null,
childVNode._original
);
} else {
childVNode = newParentVNode._children[i] = childVNode;
}

newParentVNode._children[i] = childVNode;

// Terser removes the `continue` here and wraps the loop body
// in a `if (childVNode) { ... } condition
Expand Down