React Native

React Native – 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:
import { AsyncStorage } from 'react-native';

Persist Data:
React Native AsyncStorage saves the data using setItem() method as:

AsyncStorage.setItem('key', 'value');
Example of persisting the single value:

let name = "Michal";

Example of persisting multiple values in an object:

let obj = {
name: 'Michal',
email: '',
city: 'New York',

Fetch Data:
React Native AsyncStorage fetches the saved data using getItem() method as:

await AsyncStorage.getItem('key');
Example to fetch the single value:

await AsyncStorage.getItem('user');
Example to fetch value from an object:

let user = await AsyncStorage.getItem('user');
let parsed = JSON.parse(user);


import React, {Component} from 'react';  
import {Platform, StyleSheet, Text,  
  View,TouchableOpacity, AsyncStorage,  
} from 'react-native';  
export default class App extends Component{  
    let name = "CodeHunger";  
  displayData = async ()=>{  
      let user = await AsyncStorage.getItem('user');  
  render() {  
    return (  
      <View style={styles.container}>  
        <TouchableOpacity onPress ={this.saveData}>  
          <Text>Click to save data</Text>  
        <TouchableOpacity onPress ={this.displayData}>  
          <Text>Click to display data</Text>  
const styles = StyleSheet.create({  
  container: {  
    flex: 1,  
    justifyContent: 'center',  
    alignItems: 'center',  
    backgroundColor: '#F5FCFF',  



static setItem(key: string, value: string, [callback]: ?(error: ?Error)=>void)
The setItem() sets the value for a key and invokes a callback upon compilation. It returns a Promise object.


static getItem(key: string, [callback]: ?(error: ?Error, result: ?string)) =>void)
The getItem() fetches an item from a key and invokes a callback upon completion. It returns a Promise object.


static removeItem(key: string, [callback]: ?(error: ?Error) => void)
The removeItem() removes an item for a key and invokes a callback upon compilation. It returns a Promise object.


static mergeItem(key: string, value: string, [callback]: ?(error: ?Error) => void)
The mergeItem() merges the existing key’s value with the input value and assuming both values are stringified JSON. It returns a Promise object.

NOTE: This method is not supported by all the native implementations.


static clear([callback]: ?(error: ?Error) => void)
The clear() method erases all AsynchStorage from all clients, libraries, etc. It is suggested that don’t call this, instead of this you may use removeItem or multiRemove to clear only your app’s keys. It returns the Promise object.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button