Skip to content

[object Object] displayName for React.forwardRef components #615

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

Closed
g12i opened this issue Sep 20, 2021 · 1 comment
Closed

[object Object] displayName for React.forwardRef components #615

g12i opened this issue Sep 20, 2021 · 1 comment

Comments

@g12i
Copy link

g12i commented Sep 20, 2021

React.forwardRef creates an object with a rendering function moved to a .render property (see here).

function forwardRef(render) {
  return {
     $$typeof: REACT_FORWARD_REF_TYPE,
    render,
  };
}

I'd suggest to use react-is package and copy logic from React's shared/getComponentNameFromType.js

import { isForwardRef } from "react-is";

function getWrappedName(
  outerType: mixed,
  innerType: any,
): string {
  const displayName = (outerType: any).displayName;
  if (displayName) {
    return displayName;
  }
  return innerType.displayName || innerType.name || 'No Display Name';
}


const getReactElementDisplayName = (element: mixed): string => {
  if (isForwardRef(element)) {
    return getWrappedName(element, (element as any).type);
  }
  // .. .etc
};
@armandabric
Copy link
Collaborator

This should be fixed by #617. Let me know if this is not the case. Meanwhile I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants