Skip to content

Commit 4bca77a

Browse files
committed
fix: fixed crashing on circular React elements
1 parent dbbd9e5 commit 4bca77a

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
@@ -1160,4 +1160,21 @@ describe('reactElementToJSXString(ReactElement)', () => {
11601160
}
11611161
mount(<App />);
11621162
});
1163+
1164+
it('should not crash when stringifying circular `forwardRef` element', () => {
1165+
function TagList({ tags }) {
1166+
return tags;
1167+
}
1168+
1169+
const Tag = React.forwardRef(function Tag({ text }, ref) {
1170+
return <span ref={ref}>{text}</span>;
1171+
});
1172+
Tag.emotionReal = Tag;
1173+
1174+
expect(() =>
1175+
reactElementToJSXString(
1176+
<TagList tags={[<Tag key="oops" text="oops, circular" />]} />
1177+
)
1178+
).not.toThrow();
1179+
});
11631180
});

0 commit comments

Comments
 (0)