diff --git a/src/util/object.js b/src/util/object.js index 3620489172..9f142ea4d5 100644 --- a/src/util/object.js +++ b/src/util/object.js @@ -1,3 +1,5 @@ +import React from 'react'; + /** * 获取对象的类型 * @param {*} obj @@ -249,13 +251,18 @@ export function deepMerge(target, ...sources) { if (isPlainObject(target) && isPlainObject(source)) { for (const key in source) { - if (isPlainObject(source[key])) { + // 如果是object 进行深拷贝 + if ( + isPlainObject(source[key]) && + !React.isValidElement(source[key]) + ) { if (!target[key]) Object.assign(target, { [key]: {} }); // fix {a: 'te'}, {a:{b:3}} if (!isPlainObject(target[key])) { target[key] = source[key]; } deepMerge(target[key], source[key]); + // string/number/function/react node 等直接复制 } else { Object.assign(target, { [key]: source[key] }); } diff --git a/test/util/object-spec.js b/test/util/object-spec.js index 5613c9fa88..2ce55e897c 100644 --- a/test/util/object-spec.js +++ b/test/util/object-spec.js @@ -1,3 +1,4 @@ +import React from 'react'; import assert from 'power-assert'; import * as object from '../../src/util/object'; @@ -249,6 +250,11 @@ describe('src/object.js', function() { assert(res.a === 4); }); + it('deepMerge support node', function() { + const res = object.deepMerge({}, {a: ddd}, {b: 3}); + console.log(res) + assert(Object.keys(res).length === 2); + }); it('deepMerge support deep', function() {