Asyncstorage react native что это
Note (legacy): you can use optional callback as an alternative for returned promise.
Signature:
Returns:
Promise can also be rejected in case of underlying storage error.
Example:
setItem #
Note (legacy): you can use optional callback as an alternative for returned promise.
Signature:
Returns:
Promise resolving when the set operation is completed.
Promise can also be rejected in case of underlying storage error.
Example:
mergeItem #
Signature:
Returns:
Promise with merged data, if exists, null otherwise.
Example:
removeItem #
Signature:
Returns:
Example:
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.
Signature:
Returns:
Example:
multiGet #
Fetches multiple key-value pairs for given array of keys in a batch. Once completed, invokes callback with errors (if any) and results.
Signature:
Returns:
Promise of array with coresponding key-value pairs found, stored as asyncstorage react native что это array.
Example:
multiSet #
Stores multiple key-value pairs in a batch. Once completed, callback with any errors will be called.
Signature:
Returns:
Example:
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). NOTE: This is not supported by all native implementations.
Signature:
Returns:
Example:
multiRemove #
Clears multiple key-value entries for given array of keys in a batch. Once completed, invokes a callback with errors (if any).
Signature:
Returns:
Example:
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.
Signature:
Returns:
Example:
useAsyncStorage #
Note: A hooks-like interface that we’re experimenting with. This will change in the nearest future to fully leverage Hooks API, so feel free to follow this discussion to learn more.
The useAsyncStorage returns an object that exposes all methods that allow you to interact with the stored value.
Signature:
Returns:
Specific Example:
You can replace your App.js with the following to see it in action.
🚧 AsyncStorage
Deprecated. Use one of the community packages instead.
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.
On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.
The AsyncStorage JavaScript code is a facade that provides a clear JavaScript API, real Error objects, and non-multi functions. Each method in the API returns a Promise object.
Importing the AsyncStorage library:
Reference
Methods
getItem()
Fetches an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
setItem()
Sets the value for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
removeItem()
Removes an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
mergeItem()
Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
Parameters:
clear()
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:
getAllKeys()
Gets all keys known to your app; for all callers, libraries, etc. Returns a Promise object.
Parameters:
flushGetRequests()
Flushes any pending requests using a single batch call to get the data.
multiGet()
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:
The method returns a Promise object.
Parameters:
multiSet()
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:
The method returns a Promise object.
Parameters:
multiRemove()
Call this to batch the deletion of all keys in the keys array. Returns a Promise object.
Parameters:
multiMerge()
Batch operation to merge in existing and new values for a given set of keys. This assumes that the values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
AsyncStorage
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.
On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.
The AsyncStorage JavaScript code is a facade that provides a clear JavaScript API, real Error objects, and non-multi functions. Each method in the API returns a Promise object.
Importing the AsyncStorage library:
Reference
Methods
getItem()
Fetches an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
setItem()
Sets the value for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
removeItem()
Removes an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
mergeItem()
Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
Parameters:
clear()
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:
getAllKeys()
Gets all keys known to your app; for all callers, libraries, etc. Returns a Promise object.
Parameters:
flushGetRequests()
Flushes any pending requests using a single batch call to get the data.
multiGet()
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:
The method returns a Promise object.
Parameters:
multiSet()
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:
The method returns a Promise object.
Parameters:
multiRemove()
Call this to batch the deletion of all keys in the keys array. Returns a Promise object.
Parameters:
multiMerge()
Batch operation to merge in existing and new values for a given set of keys. This assumes that the values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
AsyncStorage in React Native to Store Data in Session
React Native AsyncStorage
This is an Example to Store Data in Session Using AsyncStorage in React Native. React Native AsyncStorage can be used to manage sessions.
If you want the user to log in once and don’t want to log in again when the user opens the app after some time then, you have to store any variable in the app which can be checked and according to that, we will show the screen.
For example, once we login we can store the user id in the AsyncStorage to remember the user and when we open the app again we check for the user id, If it is there in the AsyncStorage then we can directly send the user to the next screen without login else we will show the Login Screen. This process is called session management using AsyncStorage.
AsyncStorage can only be used to store small values in the form of a key and value pair because it has some limitations to store the data. You can store your value with respect to any key and then can access the value using the key again. It works perfectly on both platforms (Android and IOS).
To Import AsyncStorage in Code
Store the value in AsyncStorage
Get the value from the AsyncStorage
In this Example. We will see the use of AsyncStorage by storing some value and after that, we will retrieve the value from the same key. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
Run the following commands to create a new React Native project
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use AsyncStorage we need to install @react-native-community/async-storage dependency.
To install this open the terminal and jump into your project using
Run the following command to install
This command will copy all the dependency into your node_module directory. –save is optional, it is just to update the @react-native-community/async-storage dependency in your package.json file.
CocoaPods Installation
After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use
Now, Open App.js in any code editor and replace the code with the following code
App.js
To Run the React Native App
Open the terminal again and jump into your project using.
To run the project on an Android Virtual Device or on real debugging device
or on the iOS Simulator by running (macOS only)
Output Screenshots


That was the React Native AsyncStorage. If you have any doubts or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!
AsyncStorage
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.
On iOS, AsyncStorage is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, AsyncStorage will use either RocksDB or SQLite based on what is available.
The AsyncStorage JavaScript code is a facade that provides a clear JavaScript API, real Error objects, and non-multi functions. Each method in the API returns a Promise object.
Importing the AsyncStorage library:
Fetches an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
Sets the value for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
Removes an item for a key and invokes a callback upon completion. Returns a Promise object.
Parameters:
Merges an existing key value with an input value, assuming both values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.
Parameters:
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:
Gets all keys known to your app; for all callers, libraries, etc. Returns a Promise object.
Parameters:
Flushes any pending requests using a single batch call to get the data.
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:
The method returns a Promise object.
Parameters:
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:
The method returns a Promise object.
Parameters:
Call this to batch the deletion of all keys in the keys array. Returns a Promise object.
Parameters:
Batch operation to merge in existing and new values for a given set of keys. This assumes that the values are stringified JSON. Returns a Promise object.
NOTE: This is not supported by all native implementations.





