22

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?

1 Answer 1

50

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/

Sign up to request clarification or add additional context in comments.

2 Comments

what if array's length is 1 mil records?
@VityaSchel On android react native async storage size has been set to 6MB so I don't think it can accommodate that much data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.