javascript - 时刻验证仅适用于 React Native 中的某些设备

标签 javascript reactjs react-native momentjs

我的 React Native 应用程序中有以下代码。

const year10To18 = (val) => {
  const birthDate = moment(new Date(val));
  const today = moment();
  const difference = today.diff(birthDate, 'years');
  return difference >= 10 && difference <= 18;
};

此验证在我的 Android 设备上正常工作,但在其他设备上不起作用?这里可能出现什么问题?

注意:我发现了这个问题。如果 React Native 调试器已打开,则验证工作正常。但如果没有这个,验证就无法进行。

还不知道原因。

最佳答案

这是由不同的 JavaScript 环境引起的。查看文档 here :

JavaScript Runtime

When using React Native, you're going to be running your JavaScript code in two environments:

  • In most cases, React Native will use JavaScriptCore, the JavaScript engine that powers Safari. Note that on iOS, JavaScriptCore does not use JIT due to the absence of writable executable memory in iOS apps.
  • When using Chrome debugging, all JavaScript code runs within Chrome itself, communicating with native code via WebSockets. Chrome uses V8 as its JavaScript engine.

While both environments are very similar, you may end up hitting some inconsistencies. We're likely going to experiment with other JavaScript engines in the future, so it's best to avoid relying on specifics of any runtime.

我认为问题可能出在日期创建上。尝试使用 moment 直接创建日期,而不是 new Date(val)。例如:时刻(2019-30-06,“YYYY-DD-MM”)

关于javascript - 时刻验证仅适用于 React Native 中的某些设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56880732/

相关文章:

JSON解析错误: Unexpected Identifier "Undefined"(React Native)

javascript - 组件渲染两次

javascript - Vue 模板对象键替代引用,无需使用键名称隐式调用

reactjs - GraphQL错误: Syntax Error: Cannot parse the unexpected character "\u00A0"

css - Less 中的&符号和双&符号有什么区别?

android - React Native Android 应用白屏无法启动

javascript - 使用媒体查询切换元素

javascript - &lt;script&gt; 元素在 SVG 文件和 HTML 文件中起作用吗?

javascript - 是否有允许多个系列/列的 D3 仪表?

javascript - 如何避免 componentDidCatch() 多次触发某些内容?