@@ -20,7 +20,7 @@ import {
20
20
ForwardRef ,
21
21
} from 'shared/ReactWorkTags' ;
22
22
23
- const ReactCurrentDispatcher = ReactSharedInternals . ReactCurrentDispatcher ;
23
+ type CurrentDispatcherRef = typeof ReactSharedInternals . ReactCurrentDispatcher ;
24
24
25
25
// Used to track hooks called during a render
26
26
@@ -408,18 +408,25 @@ function buildTree(rootStack, readHookLog): HooksTree {
408
408
export function inspectHooks < Props > (
409
409
renderFunction : Props => React$Node ,
410
410
props : Props ,
411
+ currentDispatcher : ?CurrentDispatcherRef ,
411
412
) : HooksTree {
412
- let previousDispatcher = ReactCurrentDispatcher . current ;
413
+ // DevTools will pass the current renderer's injected dispatcher.
414
+ // Other apps might compile debug hooks as part of their app though.
415
+ if ( currentDispatcher == null ) {
416
+ currentDispatcher = ReactSharedInternals . ReactCurrentDispatcher ;
417
+ }
418
+
419
+ let previousDispatcher = currentDispatcher . current ;
413
420
let readHookLog ;
414
- ReactCurrentDispatcher . current = Dispatcher ;
421
+ currentDispatcher . current = Dispatcher ;
415
422
let ancestorStackError ;
416
423
try {
417
424
ancestorStackError = new Error ( ) ;
418
425
renderFunction ( props ) ;
419
426
} finally {
420
427
readHookLog = hookLog ;
421
428
hookLog = [ ] ;
422
- ReactCurrentDispatcher . current = previousDispatcher ;
429
+ currentDispatcher . current = previousDispatcher ;
423
430
}
424
431
let rootStack = ErrorStackParser . parse ( ancestorStackError ) ;
425
432
return buildTree ( rootStack , readHookLog ) ;
@@ -450,18 +457,19 @@ function inspectHooksOfForwardRef<Props, Ref>(
450
457
renderFunction: (Props, Ref) => React$Node ,
451
458
props : Props ,
452
459
ref : Ref ,
460
+ currentDispatcher : CurrentDispatcherRef ,
453
461
) : HooksTree {
454
- let previousDispatcher = ReactCurrentDispatcher . current ;
462
+ let previousDispatcher = currentDispatcher . current ;
455
463
let readHookLog ;
456
- ReactCurrentDispatcher . current = Dispatcher ;
464
+ currentDispatcher . current = Dispatcher ;
457
465
let ancestorStackError ;
458
466
try {
459
467
ancestorStackError = new Error ( ) ;
460
468
renderFunction ( props , ref ) ;
461
469
} finally {
462
470
readHookLog = hookLog ;
463
471
hookLog = [ ] ;
464
- ReactCurrentDispatcher . current = previousDispatcher ;
472
+ currentDispatcher . current = previousDispatcher ;
465
473
}
466
474
let rootStack = ErrorStackParser.parse(ancestorStackError);
467
475
return buildTree(rootStack, readHookLog);
@@ -482,7 +490,16 @@ function resolveDefaultProps(Component, baseProps) {
482
490
return baseProps;
483
491
}
484
492
485
- export function inspectHooksOfFiber ( fiber : Fiber ) {
493
+ export function inspectHooksOfFiber (
494
+ fiber : Fiber ,
495
+ currentDispatcher : ?CurrentDispatcherRef ,
496
+ ) {
497
+ // DevTools will pass the current renderer's injected dispatcher.
498
+ // Other apps might compile debug hooks as part of their app though.
499
+ if ( currentDispatcher == null ) {
500
+ currentDispatcher = ReactSharedInternals . ReactCurrentDispatcher ;
501
+ }
502
+
486
503
if (
487
504
fiber.tag !== FunctionComponent &&
488
505
fiber . tag !== SimpleMemoComponent &&
@@ -506,9 +523,14 @@ export function inspectHooksOfFiber(fiber: Fiber) {
506
523
try {
507
524
setupContexts ( contextMap , fiber ) ;
508
525
if ( fiber . tag === ForwardRef ) {
509
- return inspectHooksOfForwardRef ( type . render , props , fiber . ref ) ;
526
+ return inspectHooksOfForwardRef (
527
+ type . render ,
528
+ props ,
529
+ fiber . ref ,
530
+ currentDispatcher ,
531
+ ) ;
510
532
}
511
- return inspectHooks(type, props);
533
+ return inspectHooks(type, props, currentDispatcher );
512
534
} finally {
513
535
currentHook = null ;
514
536
restoreContexts ( contextMap ) ;
0 commit comments