[英]App crashing while trying to use AsyncStorage
I'm trying to store a date with this: https://github.com/react-native-community/datetimepicker我正在尝试使用此存储日期: https://github.com/react-native-community/datetimepicker
The problem is that I want for the date to be saved in the AsyncStorage, possibly also to store several dates.问题是我希望将日期保存在 AsyncStorage 中,也可能存储多个日期。
This is my code:这是我的代码:
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
};
I'm not sure why this is crashing, I don't even get a chance to read the error since it crashes.我不确定为什么会崩溃,因为它崩溃了,我什至没有机会阅读错误。
The setItem
function expects a string
not an object
. setItem
function 需要一个string
而不是object
。 You should stringify the date before calling the function.您应该在调用 function 之前对日期进行字符串化。 Here is an example with get/set:
下面是一个 get/set 的例子:
AsyncStorage.setItem("my-key", JSON.stringify(date));
const date = await AsyncStorage.getItem("my-key");
console.log(new Date(date))
You can find more information in the documentation .您可以在文档中找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.