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.

Bug: React.forwardRef() wrapped component does not have it's static "displayName set by default

See original GitHub issue

I was am using enzyme for testing and react-display-name within the testing environment. As seen by this code sandbox (https://codesandbox.io/s/vigorous-chatelet-qgwwd), the display name of a stateless functional component is the name of the function. The display name of a React.forwardRef() component class is “Unknown” while the instance has the display name “ForwardRef(<function name>)” . I would love it if the resulting object that represents the forward ref definition has the same static name as the instance.

React version: 16.12.0

Steps To Reproduce

Link to code example: https://codesandbox.io/s/vigorous-chatelet-qgwwd

The current behavior

Component display name = “Unknown” Instance display name = “ForwardRef(<function name>)”

The expected behavior

Component display name = “ForwardRef(<function name>)” Instance display name = “ForwardRef(<function name>)”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
bvaughncommented, Jan 14, 2020

Perhaps that is a function of the enzyme method rather than the instance?

Yes. This sounds like Enzyme logic.

Here is what React does and what React DevTools does. Essentially:

  1. If the forward ref itself has a displayName, use it as-is.
  2. Else if the function passed to forwardRef() has a displayName, use it ¹.
  3. Else use a fallback name (e.g. just forwardRef).

¹ React and DevTools differ a little on option 2 above. DevTools returns the inner function’s displayName as-is, but React wraps a ForwardRef(...) string around it. TBH I’m more a fan of the DevTools approach (big surprise).

That being said, in neither case does React auto-add a displayName property. Those are provided by the user, opt-in. It observes them if they’re there, and uses fallbacks if they aren’t.

0reactions
bvaughncommented, Jan 14, 2020

Yup no problme.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forwarding Refs - React
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary...
Read more >
Component definition is missing display name for forwardRef
The Component definition is missing display name react/display-name message is likely coming from a tool like eslint. It is a linting rule ...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
But for people making reusable component libraries, you will need to forwardRef the underlying element, and then you can use ComponentPropsWithRef to grab...
Read more >
typescript-cheatsheet - GitHub Pages
A set of TypeScript related notes used for quick reference. The cheatsheet contains references to types, classes, decorators, and many other TypeScript ...
Read more >
Learning React once again. Yes, JSX is still here and you can…
Functional Component, is one one which accepts props and returns JSX elements. It has no state and no helper functions.
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