Skip to content

Commit 411c60e

Browse files
committed
implement cloneElement using createElement
1 parent 9091e14 commit 411c60e

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

src/clone-element.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createVNode } from './create-element';
1+
import { EMPTY_ARR } from './constants';
2+
import { createElement } from './create-element';
23

34
/**
45
* Clones the given VNode, optionally adding attributes/props and replacing its children.
@@ -8,33 +9,15 @@ import { createVNode } from './create-element';
89
* @returns {import('./internal').VNode}
910
*/
1011
export function cloneElement(vnode, props, children) {
11-
let normalizedProps = Object.assign({}, vnode.props),
12-
key,
13-
ref,
14-
i;
15-
16-
for (i in props) {
17-
if (i == 'key') key = props[i];
18-
else if (i == 'ref') ref = props[i];
19-
else normalizedProps[i] = props[i];
20-
}
21-
2212
if (arguments.length > 3) {
23-
children = [children];
24-
for (i = 3; i < arguments.length; i++) {
25-
children.push(arguments[i]);
26-
}
13+
children = EMPTY_ARR.slice.call(arguments, 2);
2714
}
2815

29-
if (children !== undefined) {
30-
normalizedProps.children = children;
31-
}
32-
33-
return createVNode(
34-
vnode.type,
35-
normalizedProps,
36-
key || vnode.key,
37-
ref || vnode.ref,
38-
0
16+
const clonedProps = Object.assign(
17+
{ key: vnode.key, ref: vnode.ref },
18+
vnode.props,
19+
props
3920
);
21+
22+
return createElement(vnode.type, clonedProps, children);
4023
}

0 commit comments

Comments
 (0)