question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component with forwardRef passed as prop

See original GitHub issue
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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, 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

0reactions
as-zlynn-philippscommented, Mar 4, 2022

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

Agreed. function noRefCheck() {} isn’t useful either. Nor is () => {} when it should be the element type (Comp).

image

Here, I’d expect onClick to be () => {} but maybe that’s a Storybook actions issue.


EDIT: Okay, so I figured out how to configure the options for this package in Storybook via global parameters.jsx.

export const parameters = {
  jsx: {
    showFunctions: true,
    // ...
  }
}

I can set showFunctions to true there, but then I run into the problem that this issue #613 is really about. Instead of the component name, it’s gobbledegook. And, since in my case, the function name is a bit different from the exported module name, that adds a layer of complexity. In my case, I might be able to leverage functionValue to achieve the desired result.

The reason onClick was showing function noRefCheck() {} is because my story uses Controls, so I’m setting the icon prop dynamically in the UI. Since sortProps is true by default, it looks like the fallback value for icon was being assigned to onClick I think? Maybe? Due to this bit in the README?

options.sortProps: boolean, default true Either to sort or not props. If you use this lib to make some isomorphic rendering you should set it to false, otherwise this would lead to react invalid checksums as the prop order is part of react isomorphic checksum algorithm.

Either way, explicitly setting sortProps to false yields a more expected result:

image

Just reporting my findings. ✌️


EDIT 2:

With the following config:

BasicUsage.parameters = {
  jsx: {
    filterProps: ['onClick'],
    functionValue: (fn: any) => fn.name.replace('Svg', ''),
    showFunctions: true,
  },
}

Voila:

image

Although, it’s kinda jank because that functionValue would apply to any function prop. It would be neat if the prop name were passed as well, that way you could modify functionValue conditionally based on the component prop. Even better, a propValue function that takes in the prop key/value and allows you to customize the output. Related: #278, #530, #682

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forwarding Refs
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >
React forwardRef: ref and other props
The order is irrelevant. forwardRef extracts the ref prop from the properties and creates a wrapper. The other props are available in props...
Read more >
How to use React's forwardRef function
Our task is to forward the ref that the Input component receives to the HTML input element. We do that by wrapping the...
Read more >
React: All you need to know about Ref Forwarding
React.forwardRef takes a function with props and ref arguments. This function returns a JSX Element. ... The props hold the props of the...
Read more >
Forwarding refs to components | by Mario Kandut
In React ref and key are handled differently. · The React.forwardRef API receives props and ref parameters and returns a React.node . ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found