Skip to content

Commit aed00da

Browse files
authored
devtools: Use context displayName for context hook name (#25954)
1 parent 118ad2a commit aed00da

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;
3939
// Used to track hooks called during a render
4040

4141
type HookLogEntry = {
42+
displayName: string | null,
4243
primitive: string,
4344
stackError: Error,
4445
value: mixed,
@@ -171,6 +172,7 @@ function use<T>(usable: Usable<T>): T {
171172
case 'fulfilled': {
172173
const fulfilledValue: T = thenable.value;
173174
hookLog.push({
175+
displayName: null,
174176
primitive: 'Promise',
175177
stackError: new Error(),
176178
value: fulfilledValue,
@@ -187,6 +189,7 @@ function use<T>(usable: Usable<T>): T {
187189
// If this was an uncached Promise we have to abandon this attempt
188190
// but we can still emit anything up until this point.
189191
hookLog.push({
192+
displayName: null,
190193
primitive: 'Unresolved',
191194
stackError: new Error(),
192195
value: thenable,
@@ -199,6 +202,7 @@ function use<T>(usable: Usable<T>): T {
199202
const value = readContext(context);
200203

201204
hookLog.push({
205+
displayName: context.displayName || 'Context',
202206
primitive: 'Context (use)',
203207
stackError: new Error(),
204208
value,
@@ -215,6 +219,7 @@ function use<T>(usable: Usable<T>): T {
215219

216220
function useContext<T>(context: ReactContext<T>): T {
217221
hookLog.push({
222+
displayName: context.displayName || null,
218223
primitive: 'Context',
219224
stackError: new Error(),
220225
value: context._currentValue,
@@ -235,6 +240,7 @@ function useState<S>(
235240
initialState()
236241
: initialState;
237242
hookLog.push({
243+
displayName: null,
238244
primitive: 'State',
239245
stackError: new Error(),
240246
value: state,
@@ -256,6 +262,7 @@ function useReducer<S, I, A>(
256262
state = init !== undefined ? init(initialArg) : ((initialArg: any): S);
257263
}
258264
hookLog.push({
265+
displayName: null,
259266
primitive: 'Reducer',
260267
stackError: new Error(),
261268
value: state,
@@ -268,6 +275,7 @@ function useRef<T>(initialValue: T): {current: T} {
268275
const hook = nextHook();
269276
const ref = hook !== null ? hook.memoizedState : {current: initialValue};
270277
hookLog.push({
278+
displayName: null,
271279
primitive: 'Ref',
272280
stackError: new Error(),
273281
value: ref.current,
@@ -279,6 +287,7 @@ function useRef<T>(initialValue: T): {current: T} {
279287
function useCacheRefresh(): () => void {
280288
const hook = nextHook();
281289
hookLog.push({
290+
displayName: null,
282291
primitive: 'CacheRefresh',
283292
stackError: new Error(),
284293
value: hook !== null ? hook.memoizedState : function refresh() {},
@@ -293,6 +302,7 @@ function useLayoutEffect(
293302
): void {
294303
nextHook();
295304
hookLog.push({
305+
displayName: null,
296306
primitive: 'LayoutEffect',
297307
stackError: new Error(),
298308
value: create,
@@ -306,6 +316,7 @@ function useInsertionEffect(
306316
): void {
307317
nextHook();
308318
hookLog.push({
319+
displayName: null,
309320
primitive: 'InsertionEffect',
310321
stackError: new Error(),
311322
value: create,
@@ -319,6 +330,7 @@ function useEffect(
319330
): void {
320331
nextHook();
321332
hookLog.push({
333+
displayName: null,
322334
primitive: 'Effect',
323335
stackError: new Error(),
324336
value: create,
@@ -341,6 +353,7 @@ function useImperativeHandle<T>(
341353
instance = ref.current;
342354
}
343355
hookLog.push({
356+
displayName: null,
344357
primitive: 'ImperativeHandle',
345358
stackError: new Error(),
346359
value: instance,
@@ -350,6 +363,7 @@ function useImperativeHandle<T>(
350363

351364
function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
352365
hookLog.push({
366+
displayName: null,
353367
primitive: 'DebugValue',
354368
stackError: new Error(),
355369
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
@@ -360,6 +374,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
360374
function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
361375
const hook = nextHook();
362376
hookLog.push({
377+
displayName: null,
363378
primitive: 'Callback',
364379
stackError: new Error(),
365380
value: hook !== null ? hook.memoizedState[0] : callback,
@@ -375,6 +390,7 @@ function useMemo<T>(
375390
const hook = nextHook();
376391
const value = hook !== null ? hook.memoizedState[0] : nextCreate();
377392
hookLog.push({
393+
displayName: null,
378394
primitive: 'Memo',
379395
stackError: new Error(),
380396
value,
@@ -395,6 +411,7 @@ function useSyncExternalStore<T>(
395411
nextHook(); // Effect
396412
const value = getSnapshot();
397413
hookLog.push({
414+
displayName: null,
398415
primitive: 'SyncExternalStore',
399416
stackError: new Error(),
400417
value,
@@ -413,6 +430,7 @@ function useTransition(): [
413430
nextHook(); // State
414431
nextHook(); // Callback
415432
hookLog.push({
433+
displayName: null,
416434
primitive: 'Transition',
417435
stackError: new Error(),
418436
value: undefined,
@@ -424,6 +442,7 @@ function useTransition(): [
424442
function useDeferredValue<T>(value: T, initialValue?: T): T {
425443
const hook = nextHook();
426444
hookLog.push({
445+
displayName: null,
427446
primitive: 'DeferredValue',
428447
stackError: new Error(),
429448
value: hook !== null ? hook.memoizedState : value,
@@ -436,6 +455,7 @@ function useId(): string {
436455
const hook = nextHook();
437456
const id = hook !== null ? hook.memoizedState : '';
438457
hookLog.push({
458+
displayName: null,
439459
primitive: 'Id',
440460
stackError: new Error(),
441461
value: id,
@@ -485,6 +505,7 @@ function useOptimistic<S, A>(
485505
state = passthrough;
486506
}
487507
hookLog.push({
508+
displayName: null,
488509
primitive: 'Optimistic',
489510
stackError: new Error(),
490511
value: state,
@@ -507,6 +528,7 @@ function useFormState<S, P>(
507528
state = initialState;
508529
}
509530
hookLog.push({
531+
displayName: null,
510532
primitive: 'FormState',
511533
stackError: new Error(),
512534
value: state,
@@ -780,7 +802,7 @@ function buildTree(
780802
}
781803
prevStack = stack;
782804
}
783-
const {primitive, debugInfo} = hook;
805+
const {displayName, primitive, debugInfo} = hook;
784806

785807
// For now, the "id" of stateful hooks is just the stateful hook index.
786808
// Custom hooks have no ids, nor do non-stateful native hooks (e.g. Context, DebugValue).
@@ -795,7 +817,7 @@ function buildTree(
795817

796818
// For the time being, only State and Reducer hooks support runtime overrides.
797819
const isStateEditable = primitive === 'Reducer' || primitive === 'State';
798-
const name = primitive === 'Context (use)' ? 'Context' : primitive;
820+
const name = displayName || primitive;
799821
const levelChild: HooksNode = {
800822
id,
801823
isStateEditable,

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,11 @@ describe('ReactHooksInspectionIntegration', () => {
772772

773773
it('should inspect the value of the current provider in useContext', () => {
774774
const MyContext = React.createContext('default');
775+
const ThemeContext = React.createContext('default');
776+
ThemeContext.displayName = 'Theme';
775777
function Foo(props) {
776778
const value = React.useContext(MyContext);
779+
React.useContext(ThemeContext);
777780
return <div>{value}</div>;
778781
}
779782
const renderer = ReactTestRenderer.create(
@@ -799,6 +802,20 @@ describe('ReactHooksInspectionIntegration', () => {
799802
"subHooks": [],
800803
"value": "contextual",
801804
},
805+
{
806+
"debugInfo": null,
807+
"hookSource": {
808+
"columnNumber": 0,
809+
"fileName": "**",
810+
"functionName": "Foo",
811+
"lineNumber": 0,
812+
},
813+
"id": null,
814+
"isStateEditable": false,
815+
"name": "Theme",
816+
"subHooks": [],
817+
"value": "default",
818+
},
802819
]
803820
`);
804821
});

0 commit comments

Comments
 (0)