From 5963b228000f9ce4f9cfb49c60db8af382058aa0 Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Tue, 8 Apr 2025 23:27:51 +0400 Subject: [PATCH 1/4] Remove check for React.isValidElement --- src/formatter/sortObject.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js index c070ef612..780226035 100644 --- a/src/formatter/sortObject.js +++ b/src/formatter/sortObject.js @@ -7,12 +7,8 @@ function safeSortObject(value: any, seen: WeakSet): any { return value; } - // return date, regexp and react element values as is - if ( - value instanceof Date || - value instanceof RegExp || - React.isValidElement(value) - ) { + // return date and regexp values as is + if (value instanceof Date || value instanceof RegExp) { return value; } From bc5f97394df3c018733596706671035bc574e688 Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Sun, 13 Apr 2025 21:38:36 +0400 Subject: [PATCH 2/4] Remove _owner key from react elements --- src/formatter/sortObject.js | 10 +++++++--- src/formatter/sortObject.spec.js | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js index 780226035..5e6b8a46a 100644 --- a/src/formatter/sortObject.js +++ b/src/formatter/sortObject.js @@ -12,6 +12,13 @@ function safeSortObject(value: any, seen: WeakSet): any { return value; } + // return react element as is but remove _owner key because it can lead to recursion + if (React.isValidElement(value)) { + const copyObj = { ...value }; + delete copyObj._owner; + return copyObj; + } + seen.add(value); // make a copy of array with each item passed through the sorting algorithm @@ -23,9 +30,6 @@ function safeSortObject(value: any, seen: WeakSet): any { return Object.keys(value) .sort() .reduce((result, key) => { - if (key === '_owner') { - return result; - } if (key === 'current' || seen.has(value[key])) { // eslint-disable-next-line no-param-reassign result[key] = '[Circular]'; diff --git a/src/formatter/sortObject.spec.js b/src/formatter/sortObject.spec.js index 1b6ab3d45..0b609dc8a 100644 --- a/src/formatter/sortObject.spec.js +++ b/src/formatter/sortObject.spec.js @@ -1,5 +1,6 @@ /* @flow */ +import React from 'react'; import sortObject from './sortObject'; describe('sortObject', () => { @@ -42,4 +43,24 @@ describe('sortObject', () => { c: date, }); }); + + it('should remove _owner key from react elements', () => { + const fixture = { + _owner: "_owner that doesn't belong to react element", + component:
, + }; + + expect(JSON.stringify(sortObject(fixture))).toEqual( + JSON.stringify({ + _owner: "_owner that doesn't belong to react element", + component: { + $$typeof: Symbol('react.transitional.element'), + type: 'div', + key: null, + props: {}, + _store: {}, + }, + }) + ); + }); }); From 311b9e8f0f1ada2e351448686f6c4aa68676da3f Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Sun, 20 Apr 2025 21:39:13 +0300 Subject: [PATCH 3/4] Change name, repo and author --- package.json | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index e92b56cf0..acb5b9a07 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "react-element-to-jsx-string", + "name": "@7rulnik/react-element-to-jsx-string", "version": "15.0.0", "description": "Turn a ReactElement into the corresponding JSX string.", "main": "dist/cjs/index.js", @@ -29,12 +29,9 @@ "git add" ] }, - "author": { - "name": "Algolia, Inc.", - "url": "https://github.com/algolia" - }, + "author": "Valentin 7rulnik Semirulnik ", "license": "MIT", - "repository": "algolia/react-element-to-jsx-string", + "repository": "7rulnik/react-element-to-jsx-string", "devDependencies": { "@babel/cli": "7.17.6", "@babel/core": "7.17.9", From 6e2700ae788f995b2eea7aedbdc421f0cc158a90 Mon Sep 17 00:00:00 2001 From: Valentin Semirulnik Date: Sun, 20 Apr 2025 21:40:03 +0300 Subject: [PATCH 4/4] 15.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index acb5b9a07..e04d4d908 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@7rulnik/react-element-to-jsx-string", - "version": "15.0.0", + "version": "15.0.1", "description": "Turn a ReactElement into the corresponding JSX string.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js",