@@ -17,6 +17,8 @@ import {type EventSubscription} from '../vendor/emitter/EventEmitter';
17
17
import { RootTagContext , createRootTag } from './RootTag' ;
18
18
import * as React from 'react' ;
19
19
20
+ const reactDevToolsHook = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
21
+
20
22
type Props = $ReadOnly < { |
21
23
children ?: React . Node ,
22
24
fabric ?: boolean ,
@@ -47,9 +49,21 @@ class AppContainer extends React.Component<Props, State> {
47
49
} ;
48
50
_mainRef : ?React . ElementRef < typeof View > ;
49
51
_subscription : ?EventSubscription = null ;
52
+ _reactDevToolsAgentListener : ?( ) = > void = null ;
50
53
51
54
static getDerivedStateFromError : any = undefined ;
52
55
56
+ mountReactDevToolsOverlays ( ) : void {
57
+ const DevtoolsOverlay = require ( '../Inspector/DevtoolsOverlay' ) . default ;
58
+ const devtoolsOverlay = < DevtoolsOverlay inspectedView = { this . _mainRef } /> ;
59
+
60
+ const TraceUpdateOverlay =
61
+ require ( '../Components/TraceUpdateOverlay/TraceUpdateOverlay' ) . default ;
62
+ const traceUpdateOverlay = < TraceUpdateOverlay /> ;
63
+
64
+ this . setState ( { devtoolsOverlay, traceUpdateOverlay} ) ;
65
+ }
66
+
53
67
componentDidMount ( ) : void {
54
68
if ( __DEV__ ) {
55
69
if ( ! this . props . internal_excludeInspector ) {
@@ -71,16 +85,21 @@ class AppContainer extends React.Component<Props, State> {
71
85
this . setState ( { inspector} ) ;
72
86
} ,
73
87
) ;
74
- if ( window . __REACT_DEVTOOLS_GLOBAL_HOOK__ != null ) {
75
- const DevtoolsOverlay =
76
- require ( '../Inspector/DevtoolsOverlay' ) . default ;
77
- const devtoolsOverlay = (
78
- < DevtoolsOverlay inspectedView = { this . _mainRef } />
88
+
89
+ if ( reactDevToolsHook != null ) {
90
+ if ( reactDevToolsHook . reactDevtoolsAgent ) {
91
+ // In case if this is not the first AppContainer rendered and React DevTools are already attached
92
+ this . mountReactDevToolsOverlays ( ) ;
93
+ return ;
94
+ }
95
+
96
+ this . _reactDevToolsAgentListener = ( ) =>
97
+ this . mountReactDevToolsOverlays ( ) ;
98
+
99
+ reactDevToolsHook . on (
100
+ 'react-devtools' ,
101
+ this . _reactDevToolsAgentListener ,
79
102
) ;
80
- const TraceUpdateOverlay =
81
- require ( '../Components/TraceUpdateOverlay/TraceUpdateOverlay' ) . default ;
82
- const traceUpdateOverlay = < TraceUpdateOverlay /> ;
83
- this . setState ( { devtoolsOverlay, traceUpdateOverlay} ) ;
84
103
}
85
104
}
86
105
}
@@ -90,6 +109,10 @@ class AppContainer extends React.Component<Props, State> {
90
109
if ( this . _subscription != null ) {
91
110
this . _subscription . remove ( ) ;
92
111
}
112
+
113
+ if ( reactDevToolsHook != null && this . _reactDevToolsAgentListener != null ) {
114
+ reactDevToolsHook . off ( 'react-devtools' , this . _reactDevToolsAgentListener ) ;
115
+ }
93
116
}
94
117
95
118
render ( ) : React . Node {
0 commit comments