Skip to content

Commit 006266d

Browse files
[utils] skip deep clone React element (v5.x) (#44494)
1 parent 6003187 commit 006266d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/mui-utils/src/deepmerge/deepmerge.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as React from 'react';
12
import { expect } from 'chai';
23
import { runInNewContext } from 'vm';
34
import deepmerge from './deepmerge';
@@ -122,4 +123,12 @@ describe('deepmerge', () => {
122123
expect(result).to.deep.equal({ foo: { baz: 'new test' } });
123124
expect(foo).to.deep.equal({ foo: { baz: 'test' } });
124125
});
126+
127+
it('should not deep clone React element', () => {
128+
const element = React.createElement('div', {}, React.createElement('span'));
129+
const element2 = React.createElement('a');
130+
const result = deepmerge({ element }, { element: element2 });
131+
132+
expect(result.element).to.equal(element2);
133+
});
125134
});

packages/mui-utils/src/deepmerge/deepmerge.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as React from 'react';
2+
13
// https://github.com/sindresorhus/is-plain-obj/blob/main/index.js
24
export function isPlainObject(item: unknown): item is Record<keyof any, unknown> {
35
if (typeof item !== 'object' || item === null) {
@@ -19,7 +21,7 @@ export interface DeepmergeOptions {
1921
}
2022

2123
function deepClone<T>(source: T): T | Record<keyof any, unknown> {
22-
if (!isPlainObject(source)) {
24+
if (React.isValidElement(source) || !isPlainObject(source)) {
2325
return source;
2426
}
2527

@@ -41,7 +43,9 @@ export default function deepmerge<T>(
4143

4244
if (isPlainObject(target) && isPlainObject(source)) {
4345
Object.keys(source).forEach((key) => {
44-
if (
46+
if (React.isValidElement(source[key])) {
47+
(output as Record<keyof any, unknown>)[key] = source[key];
48+
} else if (
4549
isPlainObject(source[key]) &&
4650
// Avoid prototype pollution
4751
Object.prototype.hasOwnProperty.call(target, key) &&

0 commit comments

Comments
 (0)