React native - Programmatically check if in remote JS debugging is enabled

后端 未结 7 782
小蘑菇
小蘑菇 2021-02-01 19:04

On React-Native, how can I know if \"Debug JS Remotely\" is enabled?

I tried looking in RN docs and various NPM packages, but couldn\'t find out how...

7条回答
  •  逝去的感伤
    2021-02-01 19:39

    Ran across this answer, but wasn't happy with checking for atob or being constrained to android. I found a function that appears to be a pretty good proxy for if the debugger is running, which is a global called __REMOTEDEV__.

    In my case, I wanted to see requests made by the app in react-native-debugger, my full code is as follows:

    /**
     * When the debugger is connected, remove the XHR polyfill
     * so that the chrome inspector will be able to see requests
     */
    if (typeof global.__REMOTEDEV__ !== 'undefined') {
      const _XHR = GLOBAL.originalXMLHttpRequest ?
          GLOBAL.originalXMLHttpRequest :
          GLOBAL.XMLHttpRequest;
    
      global.XMLHttpRequest = _XHR;
    }
    

提交回复
热议问题