Reputation: 315
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?
Upvotes: 22
Views: 34490
Reputation: 32402
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/
Upvotes: 50