React: Bug: Devtools shows Anonymous for memoized function components

Created on 21 Jan 2020  路  5Comments  路  Source: facebook/react

image

React version: 16.12.0
React dev tools version: 4.4.0

Steps To Reproduce

  1. create-react-app
  2. Render memoized function component
  3. Inspect the component with latest React dev tools chrome extension

The current behavior

Component's name in devtools is Anonymous, I've seen adding a displayName work in the past but it does not anymore.

The expected behavior

Name of component in devtools should be the set displayName.

Unconfirmed

Most helpful comment

@threepointone
Hi. I dont think its a good idea.

We use displayName to ensure we can check correct name in production environment. Not a compressed function name if use any js compress tools.

and its also have a problem of typescript is the type defintion look like allow set displayName. its return NamedExoticComponent

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/28ff2e3a70ee7172902b2efb0f0c527e8ada0430/types/react/index.d.ts#L798-L801

Please consider it, Please!

All 5 comments

I have similar issue,
default exported component shows anonymous in devtool

Screen Shot 2020-01-21 at 1 01 00 PM

I think I found a workaround. If you do SomeComponent.type.displayName = "SomeComponent" it works. Here is a codesandbox for reference: https://codesandbox.io/s/laughing-rgb-v0puv

I would not recommend mutating type. A 'proper' fix for this, is to use a named function with React.memo, like so -

const SomeComponent = React.memo(function SomeComponent(){
  // ...
})

forking your example - https://codesandbox.io/s/laughing-rgb-v0puv-c0nc5

This could be enforced with an eslint rule (similarly for forwardRef, imo)

Closing since this is expected behaviour.

@threepointone
Hi. I dont think its a good idea.

We use displayName to ensure we can check correct name in production environment. Not a compressed function name if use any js compress tools.

and its also have a problem of typescript is the type defintion look like allow set displayName. its return NamedExoticComponent

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/28ff2e3a70ee7172902b2efb0f0c527e8ada0430/types/react/index.d.ts#L798-L801

Please consider it, Please!

So did anyone fix the issue? I still see all Anonymous components when inspected. :(

Was this page helpful?
0 / 5 - 0 ratings