Categories
JavaScript Answers React Native Answers

How to see what’s stored in AsyncStorage with React Native?

Spread the love

Sometimes, we want to see what’s stored in AsyncStorage with React Native.

In this article, we’ll look at how to see what’s stored in AsyncStorage with React Native.

How to see what’s stored in AsyncStorage with React Native?

To see what’s stored in AsyncStorage with React Native, we can use the getAllKeys and multiGet methods.

For instance, we write:

import * as React from 'react';
import { AsyncStorage, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  const getEntries = async () => {
    await AsyncStorage.setItem('key', 'val');
    await AsyncStorage.setItem('key2', 'val');
    await AsyncStorage.setItem('key3', 'val');    
    const keys = await AsyncStorage.getAllKeys();
    const entries = await AsyncStorage.multiGet(keys);
    console.log(entries);
  };

  React.useEffect(() => {
    getEntries();
  }, []);

  return <View></View>;
}

to call setItem with 3 key-value entries.

Then we call getAllKeys to return all entries for all the keys.

Next, we call multiGet with keys to return an array with the entries with the given keys.

And then we call getEntries in the useEffect callback to run it.

Conclusion

To see what’s stored in AsyncStorage with React Native, we can use the getAllKeys and multiGet methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *