Documentation Index
Fetch the complete documentation index at: https://mintlify.com/facebook/react-native/llms.txt
Use this file to discover all available pages before exploring further.
AsyncStorage has been extracted from React Native core.It is now maintained in a separate repository as a community package: @react-native-async-storage/async-storageThe built-in AsyncStorage module has been deprecated and removed from React Native. Please migrate to the community package.
Migration
To migrate from the deprecated built-in AsyncStorage to the community package:
1. Install the Package
npm install @react-native-async-storage/async-storage
or
yarn add @react-native-async-storage/async-storage
2. Update Imports
Change your import statements:
Before:
import {AsyncStorage} from 'react-native';
After:
import AsyncStorage from '@react-native-async-storage/async-storage';
3. iOS Setup
For iOS, install the pods:
4. Android Setup
No additional setup required for Android. The package will be automatically linked.
Overview
AsyncStorage is an asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.
Key Features
- Asynchronous API
- Persistent storage across app restarts
- Simple key-value pairs
- Supports large data sets
- Cross-platform (iOS and Android)
Limitations
- Stores only strings (serialize objects with JSON)
- Not encrypted by default
- Storage limits vary by platform
- Not suitable for large amounts of data (consider SQLite or Realm for complex data)
Basic API
The community package maintains the same API as the original AsyncStorage:
setItem()
AsyncStorage.setItem(key: string, value: string): Promise<void>
Stores a string value for a given key.
getItem()
AsyncStorage.getItem(key: string): Promise<string | null>
Fetches a value for a given key. Returns null if not found.
removeItem()
AsyncStorage.removeItem(key: string): Promise<void>
Removes a value for a given key.
mergeItem()
AsyncStorage.mergeItem(key: string, value: string): Promise<void>
Merges an existing value with a new value (assuming both values are JSON objects).
clear()
AsyncStorage.clear(): Promise<void>
Removes all AsyncStorage data.
getAllKeys()
AsyncStorage.getAllKeys(): Promise<string[]>
Returns all keys stored in AsyncStorage.
multiGet()
AsyncStorage.multiGet(keys: string[]): Promise<[string, string | null][]>
Fetches multiple key-value pairs.
multiSet()
AsyncStorage.multiSet(keyValuePairs: [string, string][]): Promise<void>
Stores multiple key-value pairs.
multiRemove()
AsyncStorage.multiRemove(keys: string[]): Promise<void>
Removes multiple keys.
Examples
Store and Retrieve String
import AsyncStorage from '@react-native-async-storage/async-storage';
// Store data
const storeData = async (value) => {
try {
await AsyncStorage.setItem('my-key', value);
} catch (e) {
console.error('Failed to save data');
}
};
// Read data
const getData = async () => {
try {
const value = await AsyncStorage.getItem('my-key');
if (value !== null) {
return value;
}
} catch (e) {
console.error('Failed to fetch data');
}
};
Store and Retrieve Objects
import AsyncStorage from '@react-native-async-storage/async-storage';
// Store object
const storeObject = async (value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem('user-data', jsonValue);
} catch (e) {
console.error('Failed to save object');
}
};
// Read object
const getObject = async () => {
try {
const jsonValue = await AsyncStorage.getItem('user-data');
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
console.error('Failed to fetch object');
}
};
Remove Item
import AsyncStorage from '@react-native-async-storage/async-storage';
const removeValue = async () => {
try {
await AsyncStorage.removeItem('my-key');
} catch (e) {
console.error('Failed to remove item');
}
};
Get All Keys
import AsyncStorage from '@react-native-async-storage/async-storage';
const getAllKeys = async () => {
try {
const keys = await AsyncStorage.getAllKeys();
console.log('All keys:', keys);
return keys;
} catch (e) {
console.error('Failed to get keys');
}
};
Clear All Data
import AsyncStorage from '@react-native-async-storage/async-storage';
const clearAll = async () => {
try {
await AsyncStorage.clear();
console.log('Storage cleared');
} catch (e) {
console.error('Failed to clear storage');
}
};
Alternatives
For more advanced storage needs, consider:
Learn More
For complete documentation, visit the official AsyncStorage repository.