Skip to content

Commit ea73118

Browse files
authored
fix: fixed crashing on circular React elements (#619)
* fix: fixed crashing on circular React elements * chore: adjust a test implementation to be more meaningful
1 parent 2c5132e commit ea73118

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/formatter/sortObject.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/* @flow */
2+
import * as React from 'react';
23

34
export default function sortObject(value: any): any {
45
// return non-object value as is
56
if (value === null || typeof value !== 'object') {
67
return value;
78
}
89

9-
// return date and regexp values as is
10-
if (value instanceof Date || value instanceof RegExp) {
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+
) {
1116
return value;
1217
}
1318

src/index.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,4 +1334,25 @@ describe('reactElementToJSXString(ReactElement)', () => {
13341334

13351335
expect(reactElementToJSXString(<Lazy />)).toEqual(`<Lazy />`);
13361336
});
1337+
1338+
it('should stringify `forwardRef` element with a circular property', () => {
1339+
function TagList({ tags }) {
1340+
return tags;
1341+
}
1342+
1343+
const Tag = React.forwardRef(function Tag({ text }, ref) {
1344+
return <span ref={ref}>{text}</span>;
1345+
});
1346+
Tag.emotionReal = Tag;
1347+
1348+
expect(
1349+
reactElementToJSXString(
1350+
<TagList tags={[<Tag key="oops" text="oops, circular" />]} />
1351+
)
1352+
).toEqual(`<TagList
1353+
tags={[
1354+
<Tag key="oops" text="oops, circular"/>
1355+
]}
1356+
/>`);
1357+
});
13371358
});

0 commit comments

Comments
 (0)