Skip to content

Commit cd28cbd

Browse files
committed
fix: fixed crashing on circular React elements
1 parent 2c5132e commit cd28cbd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,4 +1334,21 @@ describe('reactElementToJSXString(ReactElement)', () => {
13341334

13351335
expect(reactElementToJSXString(<Lazy />)).toEqual(`<Lazy />`);
13361336
});
1337+
1338+
it('should not crash when stringifying circular `forwardRef` element', () => {
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+
).not.toThrow();
1353+
});
13371354
});

0 commit comments

Comments
 (0)