React Native — Async Storage

John Au-Yeung
DataSeries
Published in
3 min readJan 24, 2021

--

Photo by frank mckenna on Unsplash

React Native is a mobile development that’s based on React that we can use to do mobile development.

In this article, we’ll look at how to use it to create an app with React Native.

Async Storage

We can use the Async Storage library to store unencrypted data in our React Native app.

To install the library, we run:

yarn add @react-native-community/async-storage

Saving and Reading Data

Once we installed the library, we can import the library into our project and use it.

To do that, we write:

import React, { useState } from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
export default function App() {
const [data, setData] = useState();
const getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key')
if (value !== null) {
setData(value)
}
} catch (e) {
// error reading value
}
}
const storeData = async (value) => {
try {
await AsyncStorage.setItem('@storage_Key', value)
} catch (e) {
// saving error
}
}

--

--