-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Fix devtools displaying Anonymous for memo of ref-forwarding components #17274
Changes from all commits
6f2849e
04341d4
c55be00
5429c6e
6bcd757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,19 +324,39 @@ export function getInternalReactConstants( | |
PROFILER_SYMBOL_STRING, | ||
SCOPE_NUMBER, | ||
SCOPE_SYMBOL_STRING, | ||
FORWARD_REF_NUMBER, | ||
FORWARD_REF_SYMBOL_STRING, | ||
MEMO_NUMBER, | ||
MEMO_SYMBOL_STRING, | ||
} = ReactSymbols; | ||
|
||
function resolveFiberType(type: any) { | ||
// This is to support lazy components with a Promise as the type. | ||
// see https://github.com/facebook/react/pull/13397 | ||
if (typeof type.then === 'function') { | ||
return type._reactResult; | ||
} | ||
const typeSymbol = getTypeSymbol(type); | ||
switch (typeSymbol) { | ||
case MEMO_NUMBER: | ||
case MEMO_SYMBOL_STRING: | ||
// recursively resolving memo type in case of memo(forwardRef(Component)) | ||
return resolveFiberType(type.type); | ||
case FORWARD_REF_NUMBER: | ||
case FORWARD_REF_SYMBOL_STRING: | ||
return type.render; | ||
default: | ||
return type; | ||
} | ||
} | ||
|
||
// NOTICE Keep in sync with shouldFilterFiber() and other get*ForFiber methods | ||
function getDisplayNameForFiber(fiber: Fiber): string | null { | ||
const {elementType, type, tag} = fiber; | ||
|
||
// This is to support lazy components with a Promise as the type. | ||
// see https://github.com/facebook/react/pull/13397 | ||
let resolvedType = type; | ||
if (typeof type === 'object' && type !== null) { | ||
if (typeof type.then === 'function') { | ||
resolvedType = type._reactResult; | ||
} | ||
resolvedType = resolveFiberType(type); | ||
} | ||
|
||
let resolvedContext: any = null; | ||
|
@@ -350,8 +370,7 @@ export function getInternalReactConstants( | |
return getDisplayName(resolvedType); | ||
case ForwardRef: | ||
return ( | ||
resolvedType.displayName || | ||
getDisplayName(resolvedType.render, 'Anonymous') | ||
resolvedType.displayName || getDisplayName(resolvedType, 'Anonymous') | ||
); | ||
case HostRoot: | ||
return null; | ||
|
@@ -366,7 +385,7 @@ export function getInternalReactConstants( | |
if (elementType.displayName) { | ||
return elementType.displayName; | ||
} else { | ||
return getDisplayName(type, 'Anonymous'); | ||
return getDisplayName(resolvedType, 'Anonymous'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had been thinking of suggesting a different approach that just kind of localized the check: diff --git a/packages/react-devtools-shared/src/backend/renderer.js b/packages/react-devtools-shared/src/backend/renderer.js
index 761c78935..284183f9f 100644
--- a/packages/react-devtools-shared/src/backend/renderer.js
+++ b/packages/react-devtools-shared/src/backend/renderer.js
@@ -365,6 +365,9 @@ export function getInternalReactConstants(
case SimpleMemoComponent:
if (elementType.displayName) {
return elementType.displayName;
+ } else if (type.type != null && type.type.render != null) {
+ // Special case of a forwardRef() nested in a memo()
+ return getDisplayName(type.type.render, 'Anonymous');
} else {
return getDisplayName(type, 'Anonymous');
} I don't know that I really have a preference between the two approaches I guess. 😄 Yours is fine~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests added #17283 |
||
} | ||
case SuspenseComponent: | ||
return 'Suspense'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any references to
_reactResult
in the codebase. Is this still relevant? Is it safe to remove?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it has sense been renamed to
_result
now that you mention it.Someone can follow up with that in a separate PR.
cc @acdlite
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this codepath is even used by DevTools anymore. By the time React DevTools gets called for the lazy component, it just gets the e.g. function.