React Native code doesn't work without Remote Debugger enabled

后端 未结 3 1015
面向向阳花
面向向阳花 2021-02-07 18:27

This a weird question, but since I\'m really curious about this I wanted to ask. I have a piece of code that works in the iOS Simulator when I enable Remote Debugging but it sto

相关标签:
3条回答
  • This is most likely due to subtle differences between the JavaScript execution environments on the device, and in your remote debugger.

    In this case, the Date constructor seems to accept the locale-specific date formats in the Chrome remote debugging environment, but not on the device. This probably due to your computer's locale having been set to a culture that uses the dd.MM.yyyy format, and the emulator to something else, such as en-US. The ISO format works on both, because it's supported regardless of the locale.

    When you run the code on the device or simulator, the code executes in a JavaScriptCore on the device itself. This is the JavaScript engine React Native uses internally to run the application scripts

    When you turn remote debugging on, the React Native packager will instead execute the code in your computer's Chrome's JavaScript engine, V8, and send messages between the browser and the device/simulator over WebSockets.

    You've run into one of the many edge cases that make remote debugging in React Native unreliable. You should always test all features on a real device without the debugger.

    0 讨论(0)
  • 2021-02-07 19:10

    I had the same problem at first, I thought there might be something wrong within the device. Further digging into it I found it had something to do with the new Date() library.

    I used below piece of code to solve the problem :

      convDate(dateStr) {
        // From mm-dd-yyyy to yyyy-mm-ddThh:MM:ssZ
        var dArr = dateStr.split("-");
        return dArr[2] + "-" + dArr[0] + "-" + dArr[1] + "T00:00:00"; //2017-09-13T00:13:28
      }  
    

    I called the method like this:

    const check = new Date(this.convDate(givenDate));
    
    0 讨论(0)
  • 2021-02-07 19:29

    I have the same issue with dates with debugging mode moment.js fixed my issue,

    0 讨论(0)
提交回复
热议问题