简体   繁体   中英

Add new data in Async Storage React Native

Sorry to bother you but Im new at React Native. I can save data in Async Storage with a random key but when I try so save more data in a different key (dince is random) it only displays the new one. I want to display all the data. Heres my code:

When I save

const storeData = async () => {
    let id = (Math.random() + 1).toString(36).substring(7);
    var id33 = id;

    setId3(id33);
    const jsonValue = user + id33 + newdate2 + id33;

    const jsonValue4 = jsonValue;

    await AsyncStorage.setItem("user", jsonValue4);
    //await AsyncStorage.setItem("date", jsonValue2);
    //await AsyncStorage.setItem("id", jsonValue3);
    getData();
    //getData2();

    alert(jsonValue);
};

When I read:

const getData = async () => {
    const value = await AsyncStorage.getItem("user");
    const value4 = JSON.parse(value);
    setValue2(value4);
};

Because all the values you store on asyncStorage, you've stored on location 'user' and you're asking your codes to give you back data from location 'user' so it'll give you back the same value you stored in

but if you've putted other values on keys other than 'user' you can get all the keys in a array using

var keys = await AsyncStorage.getAllKeys()

and request the values using multget api of asyncStorage as below

 var keys = await AsyncStorage.getAllKeys();
 var values = await AsyncStorage.multiGet(keys);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM