AsyncStorage
AsyncStorage
, which was created as a React Native API. Amazon will deprecate the AsyncStorage
API in a future release. Kepler version 0.8 introduces the react-native-async-storage
library. Use the react-native-async-storage
library for any apps built with Kepler version 0.8 or later.AsyncStorage
is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage. It is recommended that you use an abstraction on top of AsyncStorage
instead of AsyncStorage
directly for anything more than light usage since it operates globally.
Installation
Importing the legacy version of the AsyncStorage
library:
import AsyncStorage from '@amzn/react-native-kepler';
Note: AsyncStorage
was recently removed from the mainline React Native libraries. We are working on creating a path to use AsyncStorage more idiomatically. Until this is completed, include AsyncStorage
in place of the import as follows:
const AsyncStorage = require("@amzn/react-native-kepler/Libraries/Storage/AsyncStorage");
Be aware that this imports AsyncStorage
as type any
, thus any related code will have relaxed type checking.
Persisting data:
_storeData = async () => {
try {
await AsyncStorage.setItem(
'@MySuperStore:key',
'I like to save it.',
);
} catch (error) {
// Error saving data
}
};
Fetching data:
_retrieveData = async () => {
try {
const value = await AsyncStorage.getItem('TASKS');
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};
API reference
getItem()
static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => void)
Fetches an item for a key
and invokes a callback upon completion. Returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
key | string | Yes | Key of the item to fetch. |
callback | ?(error: ?Error, result: ?string) => void |
No | Function that will be called with a result if found or any error. |
setItem()
static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void)
Sets the value for a key
and invokes a callback upon completion. Returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
key | string | Yes | Key of the item to set. |
value | string | Yes | Value to set for the key . |
callback | ?(error: ?Error) => void |
No | Function that will be called with any error. |
removeItem()
static removeItem(key: string, [callback]: ?(error: ?Error) => void)
Removes an item for a key
and invokes a callback upon completion. Returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
key | string | Yes | Key of the item to remove. |
callback | ?(error: ?Error) => void |
No | Function that will be called with any error. |
clear()
static clear([callback]: ?(error: ?Error) => void)
Erases all AsyncStorage
for all clients, libraries, etc. You probably don't want to call this; use removeItem
or multiRemove
to clear only your app's keys. Returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
callback | ?(error: ?Error) => void |
No | Function that will be called with any error. |
multiGet()
static multiGet(keys: Array<string>, [callback]: ?(errors: ?Array<Error>, result: ?Array<Array<string>>) => void)
This allows you to batch the fetching of items given an array of key
inputs. Your callback will be invoked with an array of corresponding key-value pairs found:
multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])
The method returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
keys | Array<string> |
Yes | Array of key for the items to get. |
callback | ?(errors: ?Array<Error>, result: ?Array<Array<string>>) => void |
No | Function that will be called with a key-value array of the results, plus an array of any key-specific errors found. |
Example:
AsyncStorage.getAllKeys((err, keys) => {
AsyncStorage.multiGet(keys, (err, stores) => {
stores.map((result, i, store) => {
// get at each store's key/value so you can work with it
let key = store[i][0];
let value = store[i][1];
});
});
});
multiSet()
static multiSet(keyValuePairs: Array<Array<string>>, [callback]: ?(errors: ?Array<Error>) => void)
Use this as a batch operation for storing multiple key-value pairs. When the operation completes you'll get a single callback with any errors:
multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
The method returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
keyValuePairs | Array<Array<string>> |
Yes | Array of key-value array for the items to set. |
callback | ?(errors: ?Array<Error>) => void |
No | Function that will be called with an array of any key-specific errors found. |
multiRemove()
static multiRemove(keys: Array<string>, [callback]: ?(errors: ?Array<Error>) => void)
Call this to batch the deletion of all keys in the keys
array. Returns a Promise
object.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
keys | Array<string> |
Yes | Array of key for the items to delete. |
callback | ?(errors: ?Array<Error>) => void |
No | Function that will be called an array of any key-specific errors found. |
Example:
let keys = ['k1', 'k2'];
AsyncStorage.multiRemove(keys, err => {
// keys k1 & k2 removed, if they existed
// do most stuff after removal (if you want)
});
Last updated: Sep 30, 2025