Search Docs
Async Store 的包全名为 @react-native-async-storage/async-storage,用于存放本地存储数据,类似于浏览器的 LocalStorage。
@react-native-async-storage/async-storage
1class LoggerUtils { 2 static async printStorage() { 3 console.log('------------------- Async Store -------------------') 4 const storage = await AsyncStorage.multiGet(await AsyncStorage.getAllKeys()) 5 storage.forEach(([key, value]) => { 6 console.log(`${key}: ${value}`) 7 }) 8 console.log('') 9 } 10}
如果返回了错误的对象如下:
1LOG {"_h": 0, "_i": 0, "_j": null, "_k": null}
AsyncStore.getItem() 需要使用异步的方法调用。
AsyncStore.getItem()
1// 使用 async/await 2async function getStorage(key: string) { 3 const value = await AsyncStorage.getItem(key) 4 return value 5}
1// 使用 Promise 2let value: string 3AsyncStorage.getItem(key).then((res) => { 4 value = res 5})