diff --git a/src/index.spec.js b/src/index.spec.js index e7ec29b08..3aa051042 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -725,6 +725,26 @@ describe('reactElementToJSXString(ReactElement)', () => { ); }); + it('reactElementToJSXString(
', () => { + expect( + reactElementToJSXString( +
+
+
+ ) + ).toEqual( + `
+
+
` + ); + }); + it('reactElementToJSXString(
\\n {null}\\n
', () => { const element =
{null}
; diff --git a/src/parser/parseReactElement.js b/src/parser/parseReactElement.js index 589a59169..f9d9afd1e 100644 --- a/src/parser/parseReactElement.js +++ b/src/parser/parseReactElement.js @@ -117,9 +117,20 @@ const parseReactElement = ( } const key = element.key; - if (typeof key === 'string' && key.search(/^\./)) { + if (typeof key === 'string') { + let updatedKey = key; + // React automatically add ".$" to the original key when the element is a child which has a key prop + if (key.indexOf('.$') === 0) { + updatedKey = key.slice('.$'.length); + } // React automatically add key=".X" when there are some children - props.key = key; + else if (key.indexOf('.') === 0) { + updatedKey = null; + } + + if (updatedKey) { + props.key = updatedKey; + } } const defaultProps = filterProps(element.type.defaultProps || {}, noChildren);