-
Notifications
You must be signed in to change notification settings - Fork 88
formatReactElementNode: convert displayName to String #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
If the `displayName` for a component turns out to be a `symbol` for some reason, ECMAScript will fail early when trying to implicitly convert it to a string. This explicitly converts to a string so that at least execution isn't halted.
Hi @ckknight,
Thanks for reporting this. I'm curious to understand in witch case the component displayName
could be a symbol? Did you have an example?
I just see your PR https://github.com/storybookjs/storybook/issues/9568
I can reproduce the issue with a test:
it('reactElementToJSXString(<Suspense fallback={() => "Loading..."}>Hello world</Suspense></script>)', () => { expect( reactElementToJSXString( <Suspense fallback={() => 'Loading...'}>Hello world</Suspense> ) ).toEqual( `<Suspense fallback={() => 'Loading...'}>Hello world</Suspense>` // I'm not sure we could have exactly this output ); }); /* TypeError: Cannot convert a Symbol value to a string at String.concat (<anonymous>) 120 | } = options; 121 | > 122 | let out = `<${displayName}`; | ^ 123 | 124 | let outInlineAttr = out; 125 | let outMultilineAttr = out; at _default (src/formatter/formatReactElementNode.js:122:10) at _default (src/formatter/formatTreeNode.js:50:12) at _default (src/formatter/formatTree.js:8:3) at reactElementToJsxString (src/index.js:40:10) at Object.<anonymous> (src/index.spec.js:221:7) */
xyy94813
commented
May 24, 2021
This is How React team gets component name.
https://github.com/facebook/react/blob/master/packages/shared/getComponentNameFromType.js
But, I'm not sure what the best solution for using this module.
Copy files?? or other way??
If the
displayName
for a component turns out to be asymbol
for some reason, ECMAScript will fail early when trying to implicitly convert it to a string. This explicitly converts to a string so that at least execution isn't halted.