Skip to content
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

displayName from memoized components should be taken into account in DevTools #18026

Closed
wild-lotus opened this issue Feb 12, 2020 · 10 comments
Closed

Comments

@wild-lotus
Copy link

If you create a component via React.memo and then explicitly provide it with a displayName, DevTools ignore this display name.

React version: 16.12.0
DevTools version: 4.4.0

Steps To Reproduce

  1. Create a component like:
const FancyMemoComponent = React.memo(() => 'Check my name in DevTools')
  1. Provide it with a displayName
FancyMemoComponent.displayName = 'FancyMemoComponent'
  1. Check its name in DevTools.

Link to code example: https://codesandbox.io/s/react-memo-display-name-vk7gv

The current behavior

DevTools do not display the given displayName.

The expected behavior

DevTools should display the given displayName.

Comments

I am aware that the name can be picked from the function inside React.memo, but I prefer using anonymous functions.

I am using this workaround:

FancyMemoComponent.type.displayName = 'FancyMemoComponent'
@wild-lotus wild-lotus added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 12, 2020
@moonrailgun
Copy link

Same problem in #17876

They dont recommand modify type . And its not support in typescript
I am try to persuade offical to think about it and have not reply yet

@moonrailgun
Copy link

I have a alternative solution to fix it by myself when React have not fix it

import React, { PropsWithChildren, SFC } from 'react';

export const MyMemo = <P extends object>(
  Component: SFC<P>,
  propsAreEqual?: (
    prevProps: Readonly<PropsWithChildren<P>>,
    nextProps: Readonly<PropsWithChildren<P>>
  ) => boolean
) => {
  const memoized = React.memo(Component, propsAreEqual);

  Object.defineProperty(memoized, 'displayName', {
    set(v) {
      Component.displayName = v;
    },
  });

  return memoized;
};

Its easy to replace and not change any api

@drager
Copy link

drager commented Apr 30, 2020

Would be great if the displayName become something like: Memo(ComponentName). At the moment memoized components will be displayed as _c2, _c3, etc in Chrome devtools when profiling which is somewhat cumbersome.

image

@idmadj
Copy link

idmadj commented May 4, 2020

#18495 will fix this.

@vasilii-kovalev
Copy link

vasilii-kovalev commented Aug 18, 2020

I can see that when using React.memo, why-did-you-render library displays the component's name as undefined.

const SignInForm: React.FC = React.memo(() => {
  ...
});

image

Does #18495 fix this?

@eps1lon
Copy link
Collaborator

eps1lon commented Aug 19, 2020

This issue is fixed in the latest release candidate for React 17. You can try it out with by installing react@next and react-dom@next. I created a codesandbox using displayName on React.memo to showcase the fix.

@lishoulong
Copy link

This issue is fixed in the latest release candidate for React 17. You can try it out with by installing react@next and react-dom@next. I created a codesandbox using displayName on React.memo to showcase the fix.

Hi, This bug is not fixed in your codesandbox.

@KingMatrix1989
Copy link

Not working

@Scalahansolo
Copy link

This is still an issue as outlined by the linked sandbox. Not solved.

@easyTree
Copy link

This issue is fixed in the latest release candidate for React 17. You can try it out with by installing react@next and react-dom@next. I created a codesandbox using displayName on React.memo to showcase the fix.

The App memo shows as "_c [Memo]" ([] are my attempt to simulate a block with background-color set)

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