Open
Description
const A = () => <div />
const B = React.forwardRef(() => <div />)
What I get:
<Comp
a={A}
b={{
$$typeof: Symbol(react.forward_ref),
render: _c3
}}
/>
What I expect:
<Comp
a={A}
b={B}
/>
Repro: https://codesandbox.io/s/dank-cdn-ftvik?file=/src/App.js
Activity
armandabric commentedon Sep 28, 2021
Thanks to #617 we are now supporting
React.forwardRef()
. But I still reproduce your issue with this test:As it's fresh in it's mind maybe @Andarist have an idea?
Andarist commentedon Sep 28, 2021
It's an interesting case - this is not a valid React element so it doesn't go into this logic:
react-element-to-jsx-string/src/formatter/formatPropValue.js
Lines 47 to 54 in 2c5132e
A valid React element is a result of the
React.createElement
call (so basically the output of JSX:b={<B/>}
). This is a component type~, like a class, function, result of theReact.memo
and more.armandabric commentedon Sep 30, 2021
Good point. I'm not sure what we could propose as in fact
B
is only an object parsed to a prop: not a react element.Andarist commentedon Sep 30, 2021
Well, it could still be formatted somehow - since we can recognize that it's not "just an object". But I don't believe printing
<Comp/>
is OK here. The suggested expected formatting could work OK here - printingComp
in this case. Note that in the given example we would not be able to printB
because everything is anonymous there, but with inferred names and display names something nicer could be printed.I probably won't take this work on myself though - it shouldn't be overly hard though with the groundwork being laid out already.
armandabric commentedon Sep 30, 2021
I agree with you we could easily add a dedicated render for this use case but I lack of idea on the shape of this render.
If people really need this they could explain us their use case and make us proposal. If it's a reasonable use case we will consider to handle this. I keep this issue open for the discussion.
tu4mo commentedon Sep 30, 2021
My exact use case is documenting polymorphic components with
as
prop.If the
as
component is wrapped with a forwardRef (like they usually are) it does not work.armandabric commentedon Oct 6, 2021
I see. We could maybe have a dedicated behavior for the
as
props. It's a common usageAndarist commentedon Oct 7, 2021
OTOH - this really could be handled in the same way for any prop. People probably rarely want to see the react element's internal structure in their stringified trees
iMoses-Apiiro commentedon Dec 24, 2021
I think we have two cases here to handle:
object
and has a$$typeof
property of typesymbol
, in which we can recursively search for adisplayName
function
that has adisplayName
, or fallback toname
The first case can be solved like this:
Or we can export
getReactElementDisplayName
so we can bypassparseReactElement
and get thedisplayName
directlyRegarding function components, what about this method? Could also improve regular functions display by using the
name
as fallback beforenoRefCheck
@armandabric @Andarist WDYT?
prop={Component}
breaks "show code" view storybookjs/storybook#16995as-zlynn-philipps commentedon Mar 4, 2022
Is there a PR for this? I don't see one linked.
1 remaining item