42
votes

I save some items to AsyncStorage in React Native and I am using chrome debugger and iOS simulator.

Without react native, using regular web development localStorage, I was able to see the stored localStorage items under Chrome Debugger > Resources > Local Storage

Any idea how can I view the React Native AsyncStorage stored items?

8
Not sure if such tools exist, i usually just query it and pass a callback to log it. AsyncStorage.getItem('thing').then((res) => console.log(res)) - agmcleod
Yeah, that's what I do now, but trying to see if there is a visual way to see everything in storage. - Wonka
Drop in UI component github.com/vczero/rn-cook - James

8 Answers

85
votes

React Native Debugger has this built in.

Just call showAsyncStorageContentInDev() in the RND console and you'll be able to see a dump of your app's storage.

46
votes

You can use reactotron i think it has Async Storage explorer ;) https://github.com/infinitered/reactotron

enter image description here

16
votes

Following should work,

AsyncStorage.getAllKeys((err, keys) => {
  AsyncStorage.multiGet(keys, (error, stores) => {
    stores.map((result, i, store) => {
      console.log({ [store[i][0]]: store[i][1] });
      return true;
    });
  });
});
10
votes

I have created a helper method to log all Storage in a single object (more clean to log for example in Reactotron):

import AsyncStorage from '@react-native-community/async-storage';

export function logCurrentStorage() {
  AsyncStorage.getAllKeys().then((keyArray) => {
    AsyncStorage.multiGet(keyArray).then((keyValArray) => {
      let myStorage: any = {};
      for (let keyVal of keyValArray) {
        myStorage[keyVal[0]] = keyVal[1]
      }

      console.log('CURRENT STORAGE: ', myStorage);
    })
  });
}
2
votes

With bluebird you can do this:

const dumpRaw = () => {
  return AsyncStorage.getAllKeys().then(keys => {
    return Promise.reduce(keys, (result, key) => {
      return AsyncStorage.getItem(key).then(value => {
        result[key] = value;
        return result;
      });
    }, {});
  });
};

dumpRaw().then(data => console.log(data));
1
votes

Maybe late, but none of these solutions fit for me. On android, with Android Studio open file explorer then go to data/data/your_package_name Inside you should have a folder called database and inside a file RKStorage.

This file is a SQLite3 file so get your favorite SQLite explorer and explore. If you want one this one does the job : DB Browser for SQLite

0
votes

I did not find Reactotron to have any type of pretty printing enabled and it's also brutally latent so I just wrote a simple function using lodash. You could use underscore too.

Assuming you have a static mapping of all your keys...

const keys = {
  key1: 'key1',
  key2: 'key2'
}

export function printLocalStorage() {
  _.forEach(keys, (k, v) => {
    localStore.getAllDataForKey(v).then(tree => {
      console.log(k) // Logs key above the object
      console.log(tree) // Logs a pretty printed JSON object
    })
  })
}

It's not performant but it solves the problem.

0
votes

You can Define function to get all keys by using async and await

    getAllkeys = () => {
    return new Promise( async (resolve, reject) => {
    try {
      let keys = await AsyncStorage.getAllKeys();
      let items = await AsyncStorage.multiGet(keys)
      resolve(items)
    } catch (error) {
      reject(new Error('Error getting items from AsyncStorage: ' + error.message))
    }
  });
}


    somefunc = async () => {
    try {
    var items = await getAllkeys();
    var someItems = items.filter(function (result, i, item) {
          // do filtering stuff
          return item;
    });
    // do something with filtered items 
    } catch (error) {
    // do something with your error
    }
}