-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Support displayName
on anonymous components
#3192
Conversation
🦋 Changeset detectedLatest commit: 8191ca0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@LandonSchropp added |
partial fix for #2721
Background:
For whatever reason, in react 17,
getComponentName
returns the name of the memoized component, rather than the name of the wrapper itself:https://github.com/facebook/react/blob/12adaffef7105e2714f82651ea51936c563fe15c/packages/shared/getComponentName.js#L88
So how does
displayName
work on memoized component? Memoized component definesdisplayName
as a getter/setter pair, where setter in addition to setting "own" name for the wrapper, also modifies the name of the original component:However it does it only if the original component does not have a name yet:
In this case the
Memoized.displayName
is practically useless as it's ignored by devtools (you will seeoriginal
in component tree).Problem:
Setting
displayName
(by us) on either original or memoized component prevents customizingdisplayName
by user.Solution:
Since setting
displayName
is mostly relevant for anonymous functions, don't setdisplayName
on these (we would set them to empty string anyway).Additionally, don't hoist
displayName
as it would redefine memo's getter/setter.Consequences
Component returned from
observer
does not have it's owndisplayName
, but thewrapperComponent
(available as.type
) does, which should be sufficient.