I am trying to create a save for later list, what type of storage is best for this async storage or some other method?
Is there an example of saving an array in this method?
Convert your array to a string using JSON.stringify
on save and back to an array using JSON.parse
on read:
var myArray = ['one','two','three'];
try {
await AsyncStorage.setItem('@MySuperStore:key', JSON.stringify(myArray));
} catch (error) {
// Error saving data
}
try {
const myArray = await AsyncStorage.getItem('@MySuperStore:key');
if (myArray !== null) {
// We have data!!
console.log(JSON.parse(myArray));
}
} catch (error) {
// Error retrieving data
}
Modified example from https://react-native-async-storage.github.io/async-storage/docs/install/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With