Skip to content
Advertisement

Save different values on Async Storage React Native

I’m making an app in which I’m trying to save data in the local storage of the phone. I’m using async storage for that but when I try to save a different value with a different key it just updates. Need help, please. Below is my read and write code.

 const storeData = async () => {
try {
  let id = (Math.random() + 1).toString(36).substring(7);
var id33 = id;
  const jsonValue = JSON.stringify(region);
  await AsyncStorage.setItem(id33, "Andres");
} catch (e) {
  // saving error
}

};

const fetchAllItems = async () => {
try {
  const keys = await AsyncStorage.getAllKeys();
  const items = await AsyncStorage.multiGet(keys);
  alert(items);
} catch (error) {
  console.log(error);
}

};

Thank you and sorry for bothering you.

Advertisement

Answer

Before I start console.log(e) your errors in try catch blocks when coding. Will save you time

I made a copy of your code based on the info you gave me, and everything works . Full example here (https://snack.expo.dev/dYwSij6kV) Click Editor on the very bottom then turn on panel and then click on logs to see the output.

I got the output so it is showing multiple keys.

►1:["tbl6ai","Andres"]
►2:["vd73b","Andres"]
►3:["uemcr","Andres"]
►4:["n5kie","Andres"]
►5:["EXPO_CONSTANTS_INSTALLATION_ID","3c2eee1f-f917-40ce-a9e6-ce73f83183b6"]
►6:["8a1cd","Andres"]
►7:["1d40y","Andres"]

I recommned going through every line of your code where you use async storage to see if your missing something. If that still doesn’t fix it delete your node_modules and package-lock.json and run npm install.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement