Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[AppContainer]: mount react devtools overlay only when devtools are connected #38727

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions packages/react-native/Libraries/ReactNative/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {RootTagContext, createRootTag} from './RootTag';
import * as React from 'react';

const reactDevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

type Props = $ReadOnly<{|
children?: React.Node,
fabric?: boolean,
Expand Down Expand Up @@ -45,9 +47,21 @@ class AppContainer extends React.Component<Props, State> {
};
_mainRef: ?React.ElementRef<typeof View>;
_subscription: ?EventSubscription = null;
_reactDevToolsAgentListener: ?() => void = null;

static getDerivedStateFromError: any = undefined;

mountReactDevToolsOverlays(): void {
const DevtoolsOverlay = require('../Inspector/DevtoolsOverlay').default;
const devtoolsOverlay = <DevtoolsOverlay inspectedView={this._mainRef} />;

const TraceUpdateOverlay =
require('../Components/TraceUpdateOverlay/TraceUpdateOverlay').default;
const traceUpdateOverlay = <TraceUpdateOverlay />;

this.setState({devtoolsOverlay, traceUpdateOverlay});
}

componentDidMount(): void {
if (__DEV__) {
if (!this.props.internal_excludeInspector) {
Expand All @@ -69,16 +83,21 @@ class AppContainer extends React.Component<Props, State> {
this.setState({inspector});
},
);
if (window.__REACT_DEVTOOLS_GLOBAL_HOOK__ != null) {
const DevtoolsOverlay =
require('../Inspector/DevtoolsOverlay').default;
const devtoolsOverlay = (
<DevtoolsOverlay inspectedView={this._mainRef} />

if (reactDevToolsHook != null) {
if (reactDevToolsHook.reactDevtoolsAgent) {
// In case if this is not the first AppContainer rendered and React DevTools are already attached
this.mountReactDevToolsOverlays();
return;
}

this._reactDevToolsAgentListener = () =>
this.mountReactDevToolsOverlays();

reactDevToolsHook.on(
'react-devtools',
this._reactDevToolsAgentListener,
);
const TraceUpdateOverlay =
require('../Components/TraceUpdateOverlay/TraceUpdateOverlay').default;
const traceUpdateOverlay = <TraceUpdateOverlay />;
this.setState({devtoolsOverlay, traceUpdateOverlay});
}
}
}
Expand All @@ -88,6 +107,10 @@ class AppContainer extends React.Component<Props, State> {
if (this._subscription != null) {
this._subscription.remove();
}

if (reactDevToolsHook != null && this._reactDevToolsAgentListener != null) {
reactDevToolsHook.off('react-devtools', this._reactDevToolsAgentListener);
}
}

render(): React.Node {
Expand Down