Skip to content

Commit fb63b81

Browse files
committed
[react-devtools-shared] resolve fiber type of memo and forwardRef recursively
1 parent 04341d4 commit fb63b81

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/react-devtools-shared/src/backend/renderer.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
patch as patchConsole,
5050
registerRenderer as registerRendererWithConsole,
5151
} from './console';
52+
import {isMemo, isForwardRef} from 'react-is';
5253

5354
import type {Fiber} from 'react-reconciler/src/ReactFiber';
5455
import type {
@@ -326,17 +327,28 @@ export function getInternalReactConstants(
326327
SCOPE_SYMBOL_STRING,
327328
} = ReactSymbols;
328329

330+
function resolveFiberType(type: any) {
331+
// This is to support lazy components with a Promise as the type.
332+
// see https://github.com/facebook/react/pull/13397
333+
if (typeof type.then === 'function') {
334+
return type._reactResult;
335+
}
336+
if (isMemo(type)) {
337+
return resolveFiberType(type.type);
338+
}
339+
if (isForwardRef(type)) {
340+
return resolveFiberType(type.render);
341+
}
342+
return type;
343+
}
344+
329345
// NOTICE Keep in sync with shouldFilterFiber() and other get*ForFiber methods
330346
function getDisplayNameForFiber(fiber: Fiber): string | null {
331-
const {type, tag} = fiber;
347+
const {elementType, type, tag} = fiber;
332348

333-
// This is to support lazy components with a Promise as the type.
334-
// see https://github.com/facebook/react/pull/13397
335349
let resolvedType = type;
336350
if (typeof type === 'object' && type !== null) {
337-
if (typeof type.then === 'function') {
338-
resolvedType = type._reactResult;
339-
}
351+
resolvedType = resolveFiberType(type);
340352
}
341353

342354
let resolvedContext: any = null;
@@ -348,8 +360,6 @@ export function getInternalReactConstants(
348360
case FunctionComponent:
349361
case IndeterminateComponent:
350362
return getDisplayName(resolvedType);
351-
case MemoComponent:
352-
case SimpleMemoComponent:
353363
case ForwardRef:
354364
return (
355365
resolvedType.displayName || getDisplayName(resolvedType, 'Anonymous')
@@ -362,6 +372,13 @@ export function getInternalReactConstants(
362372
case HostText:
363373
case Fragment:
364374
return null;
375+
case MemoComponent:
376+
case SimpleMemoComponent:
377+
if (elementType.displayName) {
378+
return elementType.displayName;
379+
} else {
380+
return getDisplayName(resolvedType, 'Anonymous');
381+
}
365382
case SuspenseComponent:
366383
return 'Suspense';
367384
case SuspenseListComponent:

packages/react-devtools-shared/src/utils.js

-6
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import {
2929
} from 'react-devtools-shared/src/types';
3030
import {localStorageGetItem, localStorageSetItem} from './storage';
3131

32-
import {isMemo, isForwardRef} from 'react-is';
33-
3432
import type {ComponentFilter, ElementType} from './types';
3533

3634
const cachedDisplayNames: WeakMap<Function, string> = new WeakMap();
@@ -57,10 +55,6 @@ export function getDisplayName(
5755
displayName = type.displayName;
5856
} else if (typeof type.name === 'string' && type.name !== '') {
5957
displayName = type.name;
60-
} else if (isMemo(type)) {
61-
displayName = getDisplayName(type.type);
62-
} else if (isForwardRef(type)) {
63-
displayName = getDisplayName(type.render);
6458
}
6559

6660
cachedDisplayNames.set(type, displayName);

0 commit comments

Comments
 (0)