admin管理员组文章数量:1289424
As its name suggests developer tools should be visible or accessible only during development and not in production. I don't want my end users seeing the state and ponent tree, thus knowing what's going on under the hood. Although React Developer Tool in Production neither allows modification of the ponents' state nor does it reveal their names, it doesn't fully hide them or disable the tools altogether. End Users can still see each ponent's state and the whole app tree.
Is there a way to exclude / disable React Developer tools or disconnect it in the production build, like how Augury does for Angular?
As its name suggests developer tools should be visible or accessible only during development and not in production. I don't want my end users seeing the state and ponent tree, thus knowing what's going on under the hood. Although React Developer Tool in Production neither allows modification of the ponents' state nor does it reveal their names, it doesn't fully hide them or disable the tools altogether. End Users can still see each ponent's state and the whole app tree.
Is there a way to exclude / disable React Developer tools or disconnect it in the production build, like how Augury does for Angular?
Share Improve this question asked Apr 4, 2020 at 15:28 PushkinPushkin 3,6041 gold badge19 silver badges35 bronze badges1 Answer
Reset to default 10While I was searching the internet for answers while this question is still in draft, I found this one. But sadly this didn't work. It did however tell me that it has something to do with __REACT_DEVTOOLS_GLOBAL_HOOK__
.
So After playing with it and modifying it, it worked. It successfully disconnected the App from React Developer Tools.
Here is the code to disconnect the App from React Developer Tools.
// disableReactDevTools.ts
// Declare the types if you're using TypeScript
// Ignore this block if you're using JavaScript
declare global {
interface Window {
__REACT_DEVTOOLS_GLOBAL_HOOK__: any;
}
}
export function disableReactDevTools() {
// Check if the React Developer Tools global hook exists
if (typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ !== "object") {
return;
}
for (const prop in window.__REACT_DEVTOOLS_GLOBAL_HOOK__) {
if (prop === "renderers") {
// initialise this with an empty `Map`,
// else it will throw an error in console
window.__REACT_DEVTOOLS_GLOBAL_HOOK__[prop] = new Map()
} else {
// Replace all of its properties with a no-op function or a null value
// depending on their types
window.__REACT_DEVTOOLS_GLOBAL_HOOK__[prop] =
typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__[prop] === "function"
? () => {}
: null;
}
}
}
// index.tsx
import React from "react";
import ReactDOM from "react-dom";
// ...
if (process.env.NODE_ENV === "production") disableReactDevTools();
// ...
This code will not throw any error or warnings in the console.
本文标签: javascriptHow to excludedisable React Developer Tools in Production buildStack Overflow
版权声明:本文标题:javascript - How to excludedisable React Developer Tools in Production build? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741424119a2377976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论