Skip to content

Commit 7d1cef0

Browse files
authored
Add __REACT_DEVTOOLS_HIDE_CONSOLE__ to suppress DevTools download message (#24283)
Add a global variable that allows suppressing the "Download the React DevTools" console message in environments where the Chrome extension cannot function, such as remote debugging environments and CEP-like contexts.
1 parent 7dc903c commit 7d1cef0

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

packages/react-dom/src/__tests__/ReactDOM-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,26 @@ describe('ReactDOM', () => {
477477
}
478478
});
479479

480+
it('should suppress DevTools console message when __REACT_DEVTOOLS_HIDE_CONSOLE__ is set', () => {
481+
try {
482+
global.__REACT_DEVTOOLS_HIDE_CONSOLE__ = true;
483+
jest.resetModules();
484+
485+
const infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
486+
487+
require('react-dom/client');
488+
489+
const devToolsMessage = infoSpy.mock.calls.find(call =>
490+
call[0]?.includes?.('Download the React DevTools'),
491+
);
492+
expect(devToolsMessage).toBeUndefined();
493+
494+
infoSpy.mockRestore();
495+
} finally {
496+
delete global.__REACT_DEVTOOLS_HIDE_CONSOLE__;
497+
}
498+
});
499+
480500
it('should not crash calling findDOMNode inside a function component', async () => {
481501
class Component extends React.Component {
482502
render() {

packages/react-dom/src/client/ReactDOMClient.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ export {ReactVersion as version, createRoot, hydrateRoot};
5454
const foundDevTools = injectIntoDevTools();
5555

5656
if (__DEV__) {
57-
if (!foundDevTools && canUseDOM && window.top === window.self) {
57+
if (
58+
!foundDevTools &&
59+
canUseDOM &&
60+
window.top === window.self &&
61+
typeof __REACT_DEVTOOLS_HIDE_CONSOLE__ === 'undefined'
62+
) {
5863
// If we're in Chrome or Firefox, provide a download link if not installed.
5964
if (
6065
(navigator.userAgent.indexOf('Chrome') > -1 &&

packages/react-dom/src/client/ReactDOMClientFB.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ Internals.Events /* Events */ = [
150150
const foundDevTools = injectIntoDevTools();
151151

152152
if (__DEV__) {
153-
if (!foundDevTools && canUseDOM && window.top === window.self) {
153+
if (
154+
!foundDevTools &&
155+
canUseDOM &&
156+
window.top === window.self &&
157+
typeof __REACT_DEVTOOLS_HIDE_CONSOLE__ === 'undefined'
158+
) {
154159
// If we're in Chrome or Firefox, provide a download link if not installed.
155160
if (
156161
(navigator.userAgent.indexOf('Chrome') > -1 &&

scripts/flow/environment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ declare const __REACT_DEVTOOLS_GLOBAL_HOOK__: any; /*?{
1717
inject: ?((stuff: Object) => void)
1818
};*/
1919

20+
declare const __REACT_DEVTOOLS_HIDE_CONSOLE__: void | true;
21+
2022
declare const reportError: (error: mixed) => void;
2123

2224
declare module 'create-react-class' {

0 commit comments

Comments
 (0)