react-element-to-jsx-string icon indicating copy to clipboard operation
react-element-to-jsx-string copied to clipboard

Children's key is missing

Open yoyoyooo opened this issue 6 years ago • 4 comments

Issue: There is something wrong with children's key. Example: The expected output should be

<RowItem key="1-1">
    <RadioButton 
        key="1-1-0" 
        checked={false} 
        disabled={false} 
        label="auto" 
        name="1-1" 
        tabIndex="0"
    />
</RowItem>

But I got an output without RadioButton's key:

<RowItem key="1-1">
    <RadioButton 
        checked={false} 
        disabled={false} 
        label="auto" 
        name="1-1" 
        tabIndex="0"
    />
</RowItem>

I am sure the radio button element is correct:

      { '$$typeof': Symbol(react.element),
        type: [Function: RadioButton],
        key: '1-1-0',
        ref: null,
        props: 
         { label: 'auto',
           tabIndex: '0',
           disabled: false,
           checked: false,
           name: '1-1' },
        _owner: null,
        _store: {} }

Any suggestions about this?

yoyoyooo avatar Jul 26 '18 05:07 yoyoyooo

Hello @yoyoyooo, did you have a more complete example to show us?

I just check our test suite, we have use case with some key usage: https://github.com/algolia/react-element-to-jsx-string/blob/768cce02206c52840761e901ba05ab3d46f7e1d0/src/index.spec.js#L713-L718

armandabric avatar Jul 26 '18 07:07 armandabric

I think this test case is not suitable to my problem because there is only one div. My problem is if both child and parent have its key attribute, the child's key cannot been shown. I can give another example based on your test case.

Input

reactElementToJSXString(
	<div aprop="test" key="yes">
		<div aprop="test" key="yes" />
	</div>
)

Output

<div
  key="yes"
  aprop="test"
>
  <div aprop="test" />
</div>

The inner div's key is missing.

yoyoyooo avatar Jul 26 '18 07:07 yoyoyooo

Looks like this issue is still happening in version 15.0.0:

Input:

import React from "https://esm.sh/[email protected]";
import reactElementToJsxString from "https://esm.sh/[email protected]";

console.log(reactElementToJsxString(
	<div aprop="test" key="yes">
		<div aprop="test" key="yes" />
	</div>
));

Output:

"<div
  key='yes'
  aprop='test'
>
  <div aprop='test' />
</div>"

To reproduce: https://codepen.io/danielhara/pen/GReQOPX?editors=1111

DanielHara avatar Jan 30 '24 21:01 DanielHara

@armandabric , @yoyoyooo , I've created this PR to fix this bug: https://github.com/algolia/react-element-to-jsx-string/pull/841 🚀

DanielHara avatar Jan 30 '24 22:01 DanielHara