@@ -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 ,
@@ -45,9 +47,17 @@ class AppContainer extends React.Component<Props, State> {
45
47
} ;
46
48
_mainRef : ?React . ElementRef < typeof View > ;
47
49
_subscription : ?EventSubscription = null ;
50
+ _reactDevToolsAgentListener : ?( ) = > void = null ;
48
51
49
52
static getDerivedStateFromError : any = undefined ;
50
53
54
+ mountReactDevToolsOverlays ( ) : void {
55
+ const DevtoolsOverlay = require ( '../Inspector/DevtoolsOverlay' ) . default ;
56
+ const devtoolsOverlay = < DevtoolsOverlay inspectedView = { this . _mainRef } /> ;
57
+
58
+ this . setState ( { devtoolsOverlay} ) ;
59
+ }
60
+
51
61
componentDidMount ( ) : void {
52
62
if ( __DEV__ ) {
53
63
if ( ! this . props . internal_excludeInspector ) {
@@ -69,13 +79,21 @@ class AppContainer extends React.Component<Props, State> {
69
79
this . setState ( { inspector} ) ;
70
80
} ,
71
81
) ;
72
- if ( window . __REACT_DEVTOOLS_GLOBAL_HOOK__ != null ) {
73
- const DevtoolsOverlay =
74
- require ( '../Inspector/DevtoolsOverlay' ) . default ;
75
- const devtoolsOverlay = (
76
- < DevtoolsOverlay inspectedView = { this . _mainRef } />
82
+
83
+ if ( reactDevToolsHook != null ) {
84
+ if ( reactDevToolsHook . reactDevtoolsAgent ) {
85
+ // In case if this is not the first AppContainer rendered and React DevTools are already attached
86
+ this . mountReactDevToolsOverlays ( ) ;
87
+ return ;
88
+ }
89
+
90
+ this . _reactDevToolsAgentListener = ( ) =>
91
+ this . mountReactDevToolsOverlays ( ) ;
92
+
93
+ reactDevToolsHook . on (
94
+ 'react-devtools' ,
95
+ this . _reactDevToolsAgentListener ,
77
96
) ;
78
- this . setState ( { devtoolsOverlay} ) ;
79
97
}
80
98
}
81
99
}
@@ -85,6 +103,10 @@ class AppContainer extends React.Component<Props, State> {
85
103
if ( this . _subscription != null ) {
86
104
this . _subscription . remove ( ) ;
87
105
}
106
+
107
+ if ( reactDevToolsHook != null && this . _reactDevToolsAgentListener != null ) {
108
+ reactDevToolsHook . off ( 'react-devtools' , this . _reactDevToolsAgentListener ) ;
109
+ }
88
110
}
89
111
90
112
render ( ) : React . Node {
0 commit comments