Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Flag to disable devtools #191

Closed
marcovdijk opened this issue Sep 7, 2015 · 20 comments
Closed

Flag to disable devtools #191

marcovdijk opened this issue Sep 7, 2015 · 20 comments

Comments

@marcovdijk
Copy link

Hey guys, the new devtools is really awesome! I was just wondering if there's a flag I can set in a react app to prevent non-developers from viewing or editing the app with the devtools.

@jaredly
Copy link
Contributor

jaredly commented Sep 8, 2015

It would probably work to put this at the top of your page (before react is loaded). That would effectively prevent the devtools from accessing React's context.

<script>
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function () {}
</script>

Keep in mind, though, that anybody can see & mess w/ network requests in chrome :) so if you're trying to prevent cheating, make sure you have checks server-side as well

@marcovdijk
Copy link
Author

Thanks, this is exactly what I need!

@nomadtechie
Copy link

@jaredly I'm wondering if this is still the recommended option if we want to disable react devtools for a given environment?

@jaredly
Copy link
Contributor

jaredly commented Aug 30, 2016

yup, it should work

@stueynet
Copy link

How are you guys handling mobile browsers which throw:

Uncaught TypeError: Cannot set property 'inject' of undefined

@jtlindsey
Copy link

@stueynet

try checking for __REACT_DEVTOOLS_GLOBAL_HOOK__ first

    <script>
        if (typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'object') {
            __REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function() {};
        }
    </script>

@Venryx
Copy link

Venryx commented Feb 23, 2018

I'm noticing that even when I run the code above, in a script tag before anything else, the dev-tools is still somehow getting involved and having one of its functions called during each rerender (at 60-fps). (the code above removes most of the extension's functions shown in the Profiler, but one of them still shows up)

Here is a screenshot: (the chrome-extension id shown in the function url matches the React dev-tools)

This is apparently the code that still gets involved:

/**
 * This takes care of patching the renderer to emit events on the global
 * `Hook`. The returned object has a `.cleanup` method to un-patch everything.
 */
function attachRenderer(hook, rid, renderer) {
  [...]
}

[...]

function decorate(obj, attr, fn) {
  var old = obj[attr];
  obj[attr] = function (instance) {              // <<< This is the line of the function shown in the profiler.
	var res = old.apply(this, arguments);
	fn.apply(this, arguments);
	return res;
  };
  return old;
}

[...]

module.exports = attachRenderer;
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(3)))

@Venryx
Copy link

Venryx commented Feb 23, 2018

Okay, found a way to keep even that function from getting injected/involved.

I just changed the disabler code to this:

// disable react-dev-tools for this project
if (typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === "object") {
	for (let [key, value] of Object.entries(window.__REACT_DEVTOOLS_GLOBAL_HOOK__)) {
		window.__REACT_DEVTOOLS_GLOBAL_HOOK__[key] = typeof value == "function" ? ()=>{} : null;
	}
}

@pietrofxq
Copy link

@Venryx Thanks, your code worked for me!

@scriptex
Copy link

scriptex commented Dec 3, 2018

@Venryx Thank you very much! This just works :)

For anyone who, like me, uses TypeScript, here is a TS version of the code above (extracted in a function):

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 Object.entries(DEV_TOOLS)) {
            DEV_TOOLS[key] = typeof value === 'function' ? noop : null;
        }
    }
};

And then just call the disableReactDevTools() function before you mount your app.

@sagar-gavhane
Copy link

If you want to disable in production then below snippets works fine.

if (!window.location.port && typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'object') {
  window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function () {}
}

@fvilers
Copy link

fvilers commented Feb 18, 2019

Hi there! I just want to let you know, and for future visitors, that I published a package that implements this feature so that I can resure it on all my React projects.

@max77p
Copy link

max77p commented Mar 28, 2019

@sagar-gavhane where exactly do you put this piece of code?

@Venryx
Copy link

Venryx commented Mar 28, 2019

@max77p It should be placed at the start of the first-run script, I believe. (that's where I place my variant anyway)

@fvilers
Copy link

fvilers commented Mar 29, 2019

@max77p If you want to disable the react devtools, there's an explanation on how to use the package I published here: https://github.com/fvilers/disable-react-devtools#readme

@max77p
Copy link

max77p commented Apr 8, 2019

@Venryx do you add anything else to lock down the tools or just this piece of line?

@Venryx
Copy link

Venryx commented Apr 8, 2019

@max77p No, I use just the (5/6) lines shown in my comment.

@skirankumar7
Copy link

How do I make props and state fields not editable
image

Here if you see isMobile, userLoggedIn, etc. are editalbe in production How do make not to editable.
Why isMobile and userLoggedIn came as checkboxes in devtools.
any thoughts, ideas.
please comment.
Thanks.

@pietrofxq
Copy link

@skirankumar7 You can't. If the property of the state is a boolean, it will show as checkbox in the devtools. Your only options are to remove the devtools completely with the code people posted on this issue or live with it

@skirankumar7
Copy link

Thanks @pietrofxq

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests