Skip to content

Commit 04618b0

Browse files
committed
fix(Util): copy react node directly, close #1132
1 parent 7ac35b0 commit 04618b0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/util/object.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
/**
24
* 获取对象的类型
35
* @param {*} obj
@@ -249,13 +251,18 @@ export function deepMerge(target, ...sources) {
249251

250252
if (isPlainObject(target) && isPlainObject(source)) {
251253
for (const key in source) {
252-
if (isPlainObject(source[key])) {
254+
// 如果是object 进行深拷贝
255+
if (
256+
isPlainObject(source[key]) &&
257+
!React.isValidElement(source[key])
258+
) {
253259
if (!target[key]) Object.assign(target, { [key]: {} });
254260
// fix {a: 'te'}, {a:{b:3}}
255261
if (!isPlainObject(target[key])) {
256262
target[key] = source[key];
257263
}
258264
deepMerge(target[key], source[key]);
265+
// string/number/function/react node 等直接复制
259266
} else {
260267
Object.assign(target, { [key]: source[key] });
261268
}

test/util/object-spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import assert from 'power-assert';
23
import * as object from '../../src/util/object';
34

@@ -249,6 +250,11 @@ describe('src/object.js', function() {
249250
assert(res.a === 4);
250251
});
251252

253+
it('deepMerge support node', function() {
254+
const res = object.deepMerge({}, {a: <span>ddd</span>}, {b: 3});
255+
console.log(res)
256+
assert(Object.keys(res).length === 2);
257+
});
252258

253259
it('deepMerge support deep', function() {
254260

0 commit comments

Comments
 (0)