簡體   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