React Native - Return all JSON data in AsyncStorage?
In order to get all AsyncStorage keys, you need to call AsyncStorage.getAllKeys()
. In order to speed things up, you should also use AsyncStorage.multiGet()
By doing that your code becomes;
importData = async () => { try { const keys = await AsyncStorage.getAllKeys(); const result = await AsyncStorage.multiGet(keys); return result.map(req => JSON.parse(req)).forEach(console.log); } catch (error) { console.error(error) }}
here is a more elegant way to get all items using async/await functions
const fetchAllItems = async () => { try { const keys = await AsyncStorage.getAllKeys() const items = await AsyncStorage.multiGet(keys) return items } catch (error) { console.log(error, "problemo") }}
May be, little straight forward using promises.
import { AsyncStorage } from 'react-native'; AsyncStorage.getAllKeys() .then((keys)=> AsyncStorage.multiGet(keys) .then((data) => console.log(data)));
Cheers!