What is AsyncStorage and how do you use it?

Technology CommunityCategory: React NativeWhat is AsyncStorage and how do you use it?
VietMX Staff asked 3 years ago
  • AsyncStorage is a simple, asynchronous key-value pair used in React Native applications.
  • It is a local only storage.
  • It comes in two parts: core and storage backend.
  • Core is a consumer of the storage, provides you a unified API to save and read data.
  • Storage backend implements an interface that core API understands and uses. Its functionality depends on storage itself.

Usage:

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

// Store data
storeData = async () => {
  try {
    await AsyncStorage.setItem('@storage_Key', 'stored value')
  } catch (e) {
    // saving error
  }
}

// Read data
getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}