File tree Expand file tree Collapse file tree 2 files changed +55
-9
lines changed Expand file tree Collapse file tree 2 files changed +55
-9
lines changed Original file line number Diff line number Diff line change @@ -7,15 +7,18 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
77 return value ;
88 }
99
10- // return date, regexp and react element values as is
11- if (
12- value instanceof Date ||
13- value instanceof RegExp ||
14- React . isValidElement ( value )
15- ) {
10+ // return date and regexp values as is
11+ if ( value instanceof Date || value instanceof RegExp ) {
1612 return value ;
1713 }
1814
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+
1922 seen . add ( value ) ;
2023
2124 // make a copy of array with each item passed through the sorting algorithm
@@ -27,9 +30,6 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
2730 return Object . keys ( value )
2831 . sort ( )
2932 . reduce ( ( result , key ) => {
30- if ( key === '_owner' ) {
31- return result ;
32- }
3333 if ( key === 'current' || seen . has ( value [ key ] ) ) {
3434 // eslint-disable-next-line no-param-reassign
3535 result [ key ] = '[Circular]' ;
Original file line number Diff line number Diff line change 11/* @flow */
22
3+ import React from 'react' ;
34import sortObject from './sortObject' ;
45
56describe ( 'sortObject' , ( ) => {
@@ -42,4 +43,49 @@ describe('sortObject', () => {
4243 c : date ,
4344 } ) ;
4445 } ) ;
46+
47+ describe ( '_owner key' , ( ) => {
48+ it ( 'should preserve the _owner key for objects that are not react elements' , ( ) => {
49+ const fixture = {
50+ _owner : "_owner that doesn't belong to react element" ,
51+ foo : 'bar' ,
52+ } ;
53+
54+ expect ( JSON . stringify ( sortObject ( fixture ) ) ) . toEqual (
55+ JSON . stringify ( {
56+ _owner : "_owner that doesn't belong to react element" ,
57+ foo : 'bar' ,
58+ } )
59+ ) ;
60+ } ) ;
61+
62+ it ( 'should remove the _owner key from top level react element' , ( ) => {
63+ const fixture = {
64+ reactElement : (
65+ < div >
66+ < span > </ span >
67+ </ div >
68+ ) ,
69+ } ;
70+
71+ expect ( JSON . stringify ( sortObject ( fixture ) ) ) . toEqual (
72+ JSON . stringify ( {
73+ reactElement : {
74+ type : 'div' ,
75+ key : null ,
76+ props : {
77+ children : {
78+ type : 'span' ,
79+ key : null ,
80+ props : { } ,
81+ _owner : null ,
82+ _store : { } ,
83+ } ,
84+ } ,
85+ _store : { } ,
86+ } ,
87+ } )
88+ ) ;
89+ } ) ;
90+ } ) ;
4591} ) ;
You can’t perform that action at this time.
0 commit comments