54

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?

Wonka
  • 8,244
  • 21
  • 73
  • 121
  • 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 Jul 25 '16 at 14:18
  • 1
    Yeah, that's what I do now, but trying to see if there is a visual way to see everything in storage. – Wonka Jul 25 '16 at 14:21
  • Drop in UI component https://github.com/vczero/rn-cook – James Aug 26 '16 at 19:44

11 Answers11

93

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.

Jayden
  • 998
  • 7
  • 10
53

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

enter image description here

Fareed Alnamrouti
  • 30,771
  • 4
  • 85
  • 76
  • very nice tools – Behnam Apr 23 '18 at 18:18
  • This is perfect, thanks for sharing this tool. Oh, for other greens like myself, make sure you have a `AsyncStorage.getItem(item);` – tmehta2442 May 07 '18 at 20:11
  • Hi @tmehta2442 I am not getting any log of async storage in with reactotron. can you please post how can i get all the async storage logs? I have done use(asyncStorage()) in my reactotron config. – gamingumar Apr 22 '19 at 15:11
  • @gamingumar I might be late but I have used Reactotron custom commands to do just that: `Reactotron.onCustomCommand('show_async_storage', () =>{ //here you could log all the async storage as needed })` To do the actual logging you could use @skantus answer. Here is a video explaining custom commands on Reactotron [video](https://youtu.be/UiPo9A9k7xc?t=1334) – Duilio Dec 12 '20 at 14:03
  • I also created custom commands to set and remove items. The docs aren't clear on that, but you can add parameters to them. Just add `handler: params => AsyncStorage.removeItem(params.key), args: [{name: 'key',type: ArgType.String}` – AlexDev Sep 16 '22 at 13:45
24

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;
    });
  });
});
ray
  • 5,454
  • 1
  • 18
  • 40
skantus
  • 943
  • 10
  • 21
15

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);
    })
  });
}
miqrc
  • 1,964
  • 2
  • 18
  • 24
5

react native debugger
right click on free space
enter image description here

user2177459
  • 97
  • 3
  • 9
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30572157) – MD. RAKIB HASAN Dec 14 '21 at 08:47
  • RND 0.13.0 prints this info to the Console tab. – RobinBobin Aug 29 '22 at 11:33
  • when i click "log asyncstorage content" nothing happen, no new log in console. where this log is printed ? thanks – AlainIb Apr 05 '23 at 07:41
2

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));
Tim Scott
  • 15,106
  • 9
  • 65
  • 79
  • 1
    since you are using arrow function you can use shorthand arrow function: instead of `a => {return b=>{return c}}` you can do `a=>b=>c` – Fareed Alnamrouti May 30 '17 at 19:45
2

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

Mayhem50
  • 165
  • 2
  • 8
1
import AsyncStorage from "@react-native-async-storage/async-storage";

export const printAsyncStorage = () => {
  AsyncStorage.getAllKeys((err, keys) => {
    AsyncStorage.multiGet(keys, (error, stores) => {
      let asyncStorage = {}
      stores.map((result, i, store) => {
        asyncStorage[store[i][0]] = store[i][1]
      });
      console.table(asyncStorage)
    });
  });
};

enter image description here

Shay Elbaz
  • 39
  • 3
0

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.

Max Phillips
  • 6,991
  • 9
  • 44
  • 71
0

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
    }
}
Rebai Ahmed
  • 1,509
  • 1
  • 14
  • 21
0

I have a expo snack that shows this and also performs a "load". So it is useful for doing a dump of the contents and storing it to a file and loading it up later.

Here are they parts.

const keys = await AsyncStorage.getAllKeys();
const stores = await AsyncStorage.multiGet(keys);
const data = stores.reduce(
  (acc, row) => ({ ...acc, [row[0]]: row[1] }),
  {}
);

// data now contains a JSONable Javascript object that contains all the data

This ammends the data in the AsyncStorage from a JSON string.

// sample is a JSON string
const data = JSON.parse(sample);
const keyValuePairs = Object.entries(data)
  .map(([key, value]) => [key, value])
  .reduce((acc, row) => [...acc, row], []);
await AsyncStorage.multiSet(keyValuePairs);
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265