@@ -17,6 +17,8 @@ import {type EventSubscription} from '../vendor/emitter/EventEmitter';
1717import { RootTagContext , createRootTag } from './RootTag' ;
1818import * as React from 'react' ;
1919
20+ const reactDevToolsHook = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
21+
2022type Props = $ReadOnly < { |
2123 children ?: React . Node ,
2224 fabric ?: boolean ,
@@ -47,9 +49,21 @@ class AppContainer extends React.Component<Props, State> {
4749 } ;
4850 _mainRef : ?React . ElementRef < typeof View > ;
4951 _subscription : ?EventSubscription = null ;
52+ _reactDevToolsAgentListener : ?( ) = > void = null ;
5053
5154 static getDerivedStateFromError : any = undefined ;
5255
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+
5367 componentDidMount ( ) : void {
5468 if ( __DEV__ ) {
5569 if ( ! this . props . internal_excludeInspector ) {
@@ -71,16 +85,21 @@ class AppContainer extends React.Component<Props, State> {
7185 this . setState ( { inspector} ) ;
7286 } ,
7387 ) ;
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 ,
79102 ) ;
80- const TraceUpdateOverlay =
81- require ( '../Components/TraceUpdateOverlay/TraceUpdateOverlay' ) . default ;
82- const traceUpdateOverlay = < TraceUpdateOverlay /> ;
83- this . setState ( { devtoolsOverlay, traceUpdateOverlay} ) ;
84103 }
85104 }
86105 }
@@ -90,6 +109,10 @@ class AppContainer extends React.Component<Props, State> {
90109 if ( this . _subscription != null ) {
91110 this . _subscription . remove ( ) ;
92111 }
112+
113+ if ( reactDevToolsHook != null && this . _reactDevToolsAgentListener != null ) {
114+ reactDevToolsHook . off ( 'react-devtools' , this . _reactDevToolsAgentListener ) ;
115+ }
93116 }
94117
95118 render ( ) : React . Node {
0 commit comments