Skip to content

Commit 32cdce2

Browse files
eps1lonrickhanlonii
authored andcommitted
devtools: Remove ForwardRef/Memo from display name if displayName is set (#21952)
* feat(devtools): Remove ForwardRef/Memo from display name if `displayName` is set * Avoid potentially wasting work by inlining `functionName`
1 parent 6b1f8ca commit 32cdce2

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

packages/react-devtools-shared/src/__tests__/profilingCharts-test.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ describe('profiling charts', () => {
125125
"actualDuration": 0,
126126
"didRender": true,
127127
"id": 5,
128-
"label": "Child key=\\"third\\" (<0.1ms of <0.1ms)",
129-
"name": "Child",
128+
"label": "Memo(Child) key=\\"third\\" (<0.1ms of <0.1ms)",
129+
"name": "Memo(Child)",
130130
"offset": 15,
131131
"selfDuration": 0,
132132
"treeBaseDuration": 0,
@@ -135,8 +135,8 @@ describe('profiling charts', () => {
135135
"actualDuration": 2,
136136
"didRender": true,
137137
"id": 4,
138-
"label": "Child key=\\"second\\" (2ms of 2ms)",
139-
"name": "Child",
138+
"label": "Memo(Child) key=\\"second\\" (2ms of 2ms)",
139+
"name": "Memo(Child)",
140140
"offset": 13,
141141
"selfDuration": 2,
142142
"treeBaseDuration": 2,
@@ -145,8 +145,8 @@ describe('profiling charts', () => {
145145
"actualDuration": 3,
146146
"didRender": true,
147147
"id": 3,
148-
"label": "Child key=\\"first\\" (3ms of 3ms)",
149-
"name": "Child",
148+
"label": "Memo(Child) key=\\"first\\" (3ms of 3ms)",
149+
"name": "Memo(Child)",
150150
"offset": 10,
151151
"selfDuration": 3,
152152
"treeBaseDuration": 3,
@@ -176,8 +176,8 @@ describe('profiling charts', () => {
176176
"actualDuration": 0,
177177
"didRender": false,
178178
"id": 5,
179-
"label": "Child key=\\"third\\"",
180-
"name": "Child",
179+
"label": "Memo(Child) key=\\"third\\"",
180+
"name": "Memo(Child)",
181181
"offset": 15,
182182
"selfDuration": 0,
183183
"treeBaseDuration": 0,
@@ -186,8 +186,8 @@ describe('profiling charts', () => {
186186
"actualDuration": 0,
187187
"didRender": false,
188188
"id": 4,
189-
"label": "Child key=\\"second\\"",
190-
"name": "Child",
189+
"label": "Memo(Child) key=\\"second\\"",
190+
"name": "Memo(Child)",
191191
"offset": 13,
192192
"selfDuration": 0,
193193
"treeBaseDuration": 2,
@@ -196,8 +196,8 @@ describe('profiling charts', () => {
196196
"actualDuration": 0,
197197
"didRender": false,
198198
"id": 3,
199-
"label": "Child key=\\"first\\"",
200-
"name": "Child",
199+
"label": "Memo(Child) key=\\"first\\"",
200+
"name": "Memo(Child)",
201201
"offset": 10,
202202
"selfDuration": 0,
203203
"treeBaseDuration": 3,
@@ -267,20 +267,20 @@ describe('profiling charts', () => {
267267
},
268268
Object {
269269
"id": 3,
270-
"label": "Child (Memo) key=\\"first\\" (3ms)",
271-
"name": "Child",
270+
"label": "Memo(Child) (Memo) key=\\"first\\" (3ms)",
271+
"name": "Memo(Child)",
272272
"value": 3,
273273
},
274274
Object {
275275
"id": 4,
276-
"label": "Child (Memo) key=\\"second\\" (2ms)",
277-
"name": "Child",
276+
"label": "Memo(Child) (Memo) key=\\"second\\" (2ms)",
277+
"name": "Memo(Child)",
278278
"value": 2,
279279
},
280280
Object {
281281
"id": 5,
282-
"label": "Child (Memo) key=\\"third\\" (<0.1ms)",
283-
"name": "Child",
282+
"label": "Memo(Child) (Memo) key=\\"third\\" (<0.1ms)",
283+
"name": "Memo(Child)",
284284
"value": 0,
285285
},
286286
]

packages/react-devtools-shared/src/__tests__/store-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1507,16 +1507,16 @@ describe('Store', () => {
15071507
<MyComponent> [ForwardRef]
15081508
▾ <Anonymous> [ForwardRef]
15091509
<MyComponent2>
1510-
<Custom> [ForwardRef]
1510+
<Custom>
15111511
<MyComponent4> [Memo]
15121512
▾ <MyComponent> [Memo]
15131513
<MyComponent> [ForwardRef]
15141514
<Baz> [withFoo][withBar]
15151515
<Baz> [Memo][withFoo][withBar]
15161516
<Baz> [ForwardRef][withFoo][withBar]
15171517
<Cache>
1518-
<memoRefOverride> [Memo]
1519-
<forwardRefOverride> [ForwardRef]
1518+
<memoRefOverride>
1519+
<forwardRefOverride>
15201520
`);
15211521
});
15221522

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import {
3131
deletePathInObject,
3232
getDisplayName,
33+
getWrappedDisplayName,
3334
getDefaultComponentFilters,
3435
getInObject,
3536
getUID,
@@ -451,10 +452,11 @@ export function getInternalReactConstants(
451452
case IndeterminateComponent:
452453
return getDisplayName(resolvedType);
453454
case ForwardRef:
454-
// Mirror https://github.com/facebook/react/blob/7c21bf72ace77094fd1910cc350a548287ef8350/packages/shared/getComponentName.js#L27-L37
455-
return (
456-
(type && type.displayName) ||
457-
getDisplayName(resolvedType, 'Anonymous')
455+
return getWrappedDisplayName(
456+
elementType,
457+
resolvedType,
458+
'ForwardRef',
459+
'Anonymous',
458460
);
459461
case HostRoot:
460462
const fiberRoot = fiber.stateNode;
@@ -475,10 +477,12 @@ export function getInternalReactConstants(
475477
return 'Lazy';
476478
case MemoComponent:
477479
case SimpleMemoComponent:
478-
return (
479-
(elementType && elementType.displayName) ||
480-
(type && type.displayName) ||
481-
getDisplayName(resolvedType, 'Anonymous')
480+
// Display name in React does not use `Memo` as a wrapper but fallback name.
481+
return getWrappedDisplayName(
482+
elementType,
483+
resolvedType,
484+
'Memo',
485+
'Anonymous',
482486
);
483487
case SuspenseComponent:
484488
return 'Suspense';

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ export function getAllEnumerableKeys(
101101
return keys;
102102
}
103103

104+
// Mirror https://github.com/facebook/react/blob/7c21bf72ace77094fd1910cc350a548287ef8350/packages/shared/getComponentName.js#L27-L37
105+
export function getWrappedDisplayName(
106+
outerType: mixed,
107+
innerType: any,
108+
wrapperName: string,
109+
fallbackName?: string,
110+
): string {
111+
return (
112+
(outerType: any).displayName ||
113+
`${wrapperName}(${getDisplayName(innerType, fallbackName)})`
114+
);
115+
}
116+
104117
export function getDisplayName(
105118
type: Function,
106119
fallbackName: string = 'Anonymous',
@@ -445,20 +458,6 @@ export function separateDisplayNameAndHOCs(
445458
break;
446459
}
447460

448-
if (type === ElementTypeMemo) {
449-
if (hocDisplayNames === null) {
450-
hocDisplayNames = ['Memo'];
451-
} else {
452-
hocDisplayNames.unshift('Memo');
453-
}
454-
} else if (type === ElementTypeForwardRef) {
455-
if (hocDisplayNames === null) {
456-
hocDisplayNames = ['ForwardRef'];
457-
} else {
458-
hocDisplayNames.unshift('ForwardRef');
459-
}
460-
}
461-
462461
return [displayName, hocDisplayNames];
463462
}
464463

0 commit comments

Comments
 (0)