2016-07-25 171 views
12

我在React Native中保存一些项目到AsyncStorage,我使用的是Chrome调试器和iOS模拟器。React Native - 如何查看AsyncStorage中存储的内容?

没有反应过来本地,使用常规的Web开发localStorage,我能够看到存储localStorage项目下Chrome Debugger > Resources > Local Storage

任何想法,我怎么能查看阵营本地AsyncStorage存储的项目?

+0

不知道是否存在这样的工具,我通常只是查询它并传递一个回调来记录它。 AsyncStorage.getItem('thing')。then((res)=> console.log(res))' – agmcleod

+0

是的,这就是我现在要做的,但试图看看是否有可视化的方式来查看存储中的所有内容。 – Wonka

+0

放入UI组件https://github.com/vczero/rn-cook – James

回答

2

随着bluebird你可以这样做:

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

由于您使用的是箭头功能,因此您可以使用简写箭头功能: 而不是'a => {return b => {return c}}'你可以做'a => b => c' –

0

React Native Debugger有这个建于

只需拨打showAsyncStorageContentInDev()即可D控制台,您将能够看到应用程序存储的转储。

0

我没有发现Reactotron有任何类型的漂亮打印功能,它也是残酷的潜伏,所以我只是写了一个简单的函数使用lodash。你也可以使用下划线。

假设你把所有的密钥的静态映射...

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 
    }) 
    }) 
} 

这不是高性能的,但它解决了这个问题。