react-native-async-storage
react-native-async-storage
library, which you should use for any apps built with Vega version 0.8 or later. Previous versions of Vega contained AsyncStorage
as a React Native API, which will be deprecated in a future release.@amazon-devices/react-native-async-storage__async-storage
is an extension of react-native-async-storage/async-storage that enables its use in React Native for Vega apps. The library supports an asynchronous, unencrypted, persistent, key-value storage system for React Native for Vega apps and is usable in both TypeScript and JavaScript.
This is a system-deployed library and is available to React Native for Vega apps without a separate installation process. It is deployed as an autolinking library which your app links to at runtime. Compatibility is guaranteed only between the library and the version of React Native for Vega for which it is built.
When you up-level the version of React Native for Vega with which your app is built, it a best practice to also uplevel the version of the library on which it is dependent.
Install the library
To install the react-native-async-storage
library, follow the instructions below.
- Add the JavaScript library dependency in your app's
package.json
."dependencies": { ... "@amazon-devices/react-native-async-storage__async-storage": "~2.0.2+1.23.1", "react-native-async-storage/async-storage": "1.23.1", ... }
- Reinstall
package-lock.json
file usingnpm install
command.
Examples
Storing data
Storing string value
import React from "react";
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
await AsyncStorage.setItem('my-key', value);
} catch (e) {
// saving error
}
};
Storing object value
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem('my-key', jsonValue);
} catch (e) {
// saving error
}
};
Reading data
Reading string value
import AsyncStorage from '@react-native-async-storage/async-storage';
const getData = async () => {
try {
const value = await AsyncStorage.getItem('my-key');
if (value !== null) {
// value previously stored
}
} catch (e) {
// error reading value
}
};
Reading object value
import AsyncStorage from '@react-native-async-storage/async-storage';
const getData = async () => {
try {
const jsonValue = await AsyncStorage.getItem('my-key');
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
}
};
API reference
Prop | Description | Default |
---|---|---|
getItem |
Gets a string value for given key. This function can either return a string value for existing key or return null otherwise. In order to store object value, you need to deserialize it, e.g. using JSON.parse(). Note (legacy): you can use optional callback as an alternative for returned promise. |
None |
setItem |
Sets a string value for given key. This operation can either modify an existing entry, if it did exist for given key, or add new one otherwise. In order to store object value, you need to serialize it, e.g. using JSON.stringify(). Note (legacy): you can use optional callback as an alternative for returned promise. |
None |
mergeItem |
Merges an existing value stored under key, with new value, assuming both values are stringified JSON. | None |
removeItem |
Removes item for a key, invokes (optional) callback once completed. | None |
getAllKeys |
Returns all keys known to your App, for all callers, libraries, etc. Once completed, invokes callback with errors (if any) and array of keys. | None |
multiGet |
Fetches multiple key-value pairs for given array of keys in a batch. Once completed, invokes callback with errors (if any) and results. | None |
multiSet |
Stores multiple key-value pairs in a batch. Once completed, callback with any errors will be called. | None |
multiMerge |
Multiple merging of existing and new values in a batch. Assumes that values are stringified JSON. Once completed, invokes callback with errors (if any). | None |
multiRemove |
Clears multiple key-value entries for given array of keys in a batch. Once completed, invokes a callback with errors (if any). | None |
clear |
Removes whole AsyncStorage data, for all clients, libraries, etc. You probably want to use removeItem or multiRemove to clear only your App's keys. | None |
useAsyncStorage |
Note: An experimental hooks-like interface. This will change in the nearest future to fully leverage Hooks API, so feel free to follow this discussion to learn more. The useAsyncStorage hook returns an object that exposes all methods that allow you to interact with the stored value. |
None |
For more details and usage examples, please refer to the react-native-async-storage/async-storage library's documentation.
Supported versions
Package name | Amazon NPM library version | Vega OS build number | Vega SDK version | Release notes |
---|---|---|---|---|
@amazon-devices/react-native-async-storage__async-storage |
2.0.0+2.13.0 | OS 1.1 (201010438050) |
0.20 |
Additional resources
For information on additional libraries, see Supported Third-Party Libraries and Services.
Last updated: Sep 30, 2025