How can I view network requests (for debugging) in React Native?

前端 未结 12 1195
借酒劲吻你
借酒劲吻你 2020-11-28 03:16

I\'d like to view my network requests in React Native to help me debug - ideally in the \'Network\' tab of Chrome\'s devtools.

There are some closed issues about thi

12条回答
  •  不知归路
    2020-11-28 03:31

    This is what I've been using in the entry point of my app

    const _XHR = GLOBAL.originalXMLHttpRequest ?  
        GLOBAL.originalXMLHttpRequest :           
        GLOBAL.XMLHttpRequest                     
    
    XMLHttpRequest = _XHR
    

    EDIT: frevib linked to more concise syntax below. Thanks frevib!

    GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest;
    

    Explanation:

    GLOBAL.originalXMLHttpRequest refers the Chrome Dev Tools copy of XHR. It is provided by RN as an escape hatch. Shvetusya's solution will only work if the dev tools are open and thus providing XMLHttpRequest.

    EDIT: You will need to allow cross origin requests when in debugger mode. With chrome you can use this handy plugin.

    EDIT: Read about the RN github issue that lead me to this solution

提交回复
热议问题