繁体   English   中英

尝试使用 AsyncStorage 时应用程序崩溃

[英]App crashing while trying to use AsyncStorage

我正在尝试使用此存储日期: https://github.com/react-native-community/datetimepicker

问题是我希望将日期保存在 AsyncStorage 中,也可能存储多个日期。

这是我的代码:

const onChange = async(event, selectedDate) => {
    const currentDate = selectedDate || date;
    setShow(Platform.OS === 'ios');
    setDate(currentDate);
    await AsyncStorage.setItem('Date', selectedDate); // the problem is here, I believe
}; 

我不确定为什么会崩溃,因为它崩溃了,我什至没有机会阅读错误。

setItem function 需要一个string而不是object 您应该在调用 function 之前对日期进行字符串化。 下面是一个 get/set 的例子:

AsyncStorage.setItem("my-key", JSON.stringify(date));

const date = await AsyncStorage.getItem("my-key");
console.log(new Date(date))

您可以在文档中找到更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM