某些代码仅在 chrome 调试器处于活动状态时才有效 [英] Some code only works while chrome debugger is active
问题描述
我正在开发一个 react-native 应用程序,我正在尝试使用 moment 格式化日期.
I'm working on a react-native application and I'm trying to format a date using moment.
日期看起来像02-16-2016 09:04:23"
Dates look like "02-16-2016 09:04:23"
function formatTime(date){
var formattedDate = moment(date).format('MM:ss A');
return formattedDate;
}
如果 chrome 调试器处于活动状态,则工作正常.但是如果我禁用它,我得到的只是无效日期"
Works fine if the chrome debugger is active. But if I disable it, all I get is "invalid date"
与我正在使用的解码功能相同
Same thing with a decoding function I'm using
var that = this;
MessagesService.getMessageBody(selectedMessage)
.then(function(messageBody){
var decodedData = window.atob(messageBody.messages);
that.setState({
messageBody: decodedData
})
})
.catch(function(err){
console.log(err);
})
显示解码数据
<Text> Body: {this.state.messageBody} </Text>
并用
<View style = {[MessageStyles.senderItem, MessageStyles.date]}>
<Text>
{this.formatTime(message.createDateTime)}
</Text>
</View>
也许这是在 React Native 中执行此操作的一种糟糕方法?仍在学习,所以我可能会做一些不好的练习.
Maybe this is a bad way to do this in react native? Still learning so I could be doing some bad practice.
推荐答案
我最近了解到在使用 chrome 调试器时,react native 使用不同的 JS 引擎.调试时使用 chrome JS 引擎,否则使用 JavaScriptCore.根据这篇文章
I learned recently that when using the chrome debugger, react native uses a different JS engine. The chrome JS engine is used during debugging, but JavaScriptCore is used otherwise. Per this article
http://ruoyusun.com/2015/11/01/things-i-wish-i-were-told-about-react-native.html
但对于日期的实际问题,JavaScriptCore 引擎似乎不喜欢用 - 解析日期.我不得不使用正则表达式将 - 替换为/,然后我的所有日期操作都可以正常工作.
But for the actual issue with the dates, the JavaScriptCore engine doesn't seem to like parsing dates with a -. I had to use a regex expression to replace the - with a / and then all my date manipulation worked fine.
02-16-2016 09:04:23 视为无效
02-16-2016 09:04:23 is considered invalid
02/16/2016 09:04:23 被认为是有效的
02/16/2016 09:04:23 is considered valid
这篇关于某些代码仅在 chrome 调试器处于活动状态时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!