在未启用远程调试器的情况下,React本机代码无法正常工作 [英] React Native code doesn't work without Remote Debugger enabled

查看:116
本文介绍了在未启用远程调试器的情况下,React本机代码无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的问题,但是由于我对此很好奇,所以我想问一下.当我启用远程调试时,我有一段代码可在iOS模拟器中使用,但没有它,它将停止工作.这是代码:

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 stops working without it. This is the code:

      let filtered = []
      let dueDate
      const dateNow = new Date(Date.now())

      for (let item of this.props.listData) {
        dueDate = new Date(item.dueDate)
        if (!item.paid && (dueDate < dateNow)) {
          filtered.push(item)
        }
      }
      if (filtered.length > 0) {          
        this.setState({
          dataSource: this.state.dataSource.cloneWithRows(filtered)
        })
      }

(item.dueDate是类似于"12.02.2016"的字符串)

(item.dueDate is a string like "12.02.2016")

很明显,如果我以正确的格式(例如ISO"2016-02-12")提供日期,则代码可以工作(并且可以工作).我感兴趣的是为什么启用远程调试(启动Google Chrome实例)时即使格式错误也能正常工作吗?因为当我在Chrome浏览器中console.log记录日期时,它们可以正常转换.预先感谢!

Obviously the code would work (and it works) if I supplied the date in a correct format (like ISO "2016-02-12"). What interests me is why does it work even with the wrong format when I enable Remote Debugging (which fires up a Google Chrome instance)? Because when I console.log the dates in Chrome they are converted normally. Thanks in advance!

推荐答案

这很可能是由于设备和远程调试器中的JavaScript执行环境之间的细微差异.

This is most likely due to subtle differences between the JavaScript execution environments on the device, and in your remote debugger.

在这种情况下,Date构造函数似乎在Chrome远程调试环境中接受特定于语言环境的日期格式,但在设备上不接受.这可能是由于计算机的语言环境已设置为使用dd.MM.yyyy格式的区域性,而模拟器已设置为其他名称,例如en-US. ISO格式可同时在这两种格式上使用,因为不受区域设置的支持.

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.

当您在设备或模拟器上运行代码时,代码将在 JavaScriptCore 中执行设备本身.这是React Native在内部用于运行应用程序脚本的JavaScript引擎

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

当您打开远程调试时,React Native打包程序将改为在您计算机的Chrome的JavaScript引擎中执行代码, V8 ,并通过WebSocket在浏览器和设备/模拟器之间发送消息.

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.

您遇到了许多使React Native中的远程调试不可靠的极端情况之一.您应该始终在没有调试器的情况下在真实设备上测试所有功能.

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.

这篇关于在未启用远程调试器的情况下,React本机代码无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆