简体   繁体   中英

App crashing while trying to use AsyncStorage

I'm trying to store a date with this: 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.

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 . You should stringify the date before calling the function. Here is an example with 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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