Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you save an array in async storage in react native ?

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?

like image 616
CRod Avatar asked Jul 17 '16 00:07

CRod


1 Answers

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/

like image 162
FuzzyTree Avatar answered Sep 17 '22 03:09

FuzzyTree