Skip to content

Commit 4ee3b94

Browse files
committed
Remove _owner key from react elements
1 parent 1b7d5b8 commit 4ee3b94

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/formatter/sortObject.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
1212
return value;
1313
}
1414

15+
// return react element as is but remove _owner key because it can lead to recursion
16+
if (React.isValidElement(value)) {
17+
const copyObj = { ...value };
18+
delete copyObj._owner;
19+
return copyObj;
20+
}
21+
1522
seen.add(value);
1623

1724
// make a copy of array with each item passed through the sorting algorithm
@@ -23,9 +30,6 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
2330
return Object.keys(value)
2431
.sort()
2532
.reduce((result, key) => {
26-
if (key === '_owner') {
27-
return result;
28-
}
2933
if (key === 'current' || seen.has(value[key])) {
3034
// eslint-disable-next-line no-param-reassign
3135
result[key] = '[Circular]';

src/formatter/sortObject.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
import React from 'react';
34
import sortObject from './sortObject';
45

56
describe('sortObject', () => {
@@ -42,4 +43,24 @@ describe('sortObject', () => {
4243
c: date,
4344
});
4445
});
46+
47+
it('should remove _owner key from react elements', () => {
48+
const fixture = {
49+
_owner: "_owner that doesn't belong to react element",
50+
component: <div />,
51+
};
52+
53+
expect(JSON.stringify(sortObject(fixture))).toEqual(
54+
JSON.stringify({
55+
_owner: "_owner that doesn't belong to react element",
56+
component: {
57+
$$typeof: Symbol('react.transitional.element'),
58+
type: 'div',
59+
key: null,
60+
props: {},
61+
_store: {},
62+
},
63+
})
64+
);
65+
});
4566
});

0 commit comments

Comments
 (0)