Skip to content

Instantly share code, notes, and snippets.

@azl397985856
Created August 5, 2019 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azl397985856/5d60f0cd89a2d50050d7cb0376aaae87 to your computer and use it in GitHub Desktop.
Save azl397985856/5d60f0cd89a2d50050d7cb0376aaae87 to your computer and use it in GitHub Desktop.
生产模式下禁止React Developer Tools、Redux DevTools的使用

React Developer Tools

window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function () {}
import {
  disableReactDevTools
} from '@utils/js/other';
if(process.env.NODE_ENV == 'production'){
  disableReactDevTools();
}
// @utils/js/other.ts
export const disableReactDevTools = (): void => {
    const noop = (): void => undefined;
    const DEV_TOOLS = (window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__;

    if (typeof DEV_TOOLS === 'object') {
        for (const [key, value] of (<any>Object).entries(DEV_TOOLS)) {
            DEV_TOOLS[key] = typeof value === 'function' ? noop : null;
        }
    }
};

禁止Redux DevTools

import {
    composeWithDevTools
} from 'redux-devtools-extension/developmentOnly';

// other code...
const store = createStore(
    rootReducer,
    composeWithDevTools(middlewareEnhancer)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment