React Native AsyncStorage
React Native AsyncStorage is a simple, unencrypted, asynchronous, persistent, storage system which stores the data globally in the app. It store data in the form of a key-value pair.
React Native recommended to use abstraction on top of AsyncStorage instead of AsyncStorage directly as it operates globally.
On iOS, AsyncStorage is approved by the native code. The iOS native code stores the small values in a serialized dictionary and the larger values in separate files.
On Android, AsyncStorage will use either SQLite or RocksDB based on the availability.
To use the AsyncStorage, import AsyncStorage library as:
React Native AsyncStorage saves the data using setItem() method as:
Example of persisting the single value:
Example of persisting multiple values in an object:
React Native AsyncStorage fetches the saved data using getItem() method as:
Example to fetch the single value:
Example to fetch value from an object:
React Native AsyncStorage Example 1
In this example, we create the two TouchableOpacity components, one for saving the data and another for retrieving. From first TouchableOpacity component call the savaData() method to save data and from the second TouchableOpacity component call the displayData() method to fetch data.
React Native AsyncStorage Example 2