create a project using npx create-expo-app@latest —template

choose your desired template, for this video, i selected blank one and i would recommend you too do the same.

✈️ Steps To use

Step 1: Install the community package :

https://react-native-async-storage.github.io/async-storage/

use this command to install the package

npm install @react-native-async-storage/async-storage

<aside> 💡

if you are using expo, no additional step is required but in case of react native cli, you need to do pod install

for additional information on installation refer the docs : https://react-native-async-storage.github.io/async-storage/docs/install

</aside>

Step 2 : import

import AsyncStorage from '@react-native-async-storage/async-storage';

Step 3: Store/ Save data

the data you want to save can be taken from user inputs or it can be hardcoded values as well. Depends on what you are trying to store and what is the usecase.

In this simple example, we are trying to store a value user enetered via text input

const saveData = async () => {
		try {
			await AsyncStorage.setItem("user", input);
			alert("Data Saved!");
		} catch (error) {
			console.log(error);
		}
	};

Step 4 : Get/ fetch data from async storage

<aside> 💡

make sure you use the same key name while fetching data from async storage

</aside>

const getData = async () => {
		try {
			const value = await AsyncStorage.getItem("user");
			if (value !== null) {
				setStoredData(value);
			}
		} catch (error) {
			console.log(error);
		}
	};

Additional Points

If you want that as soon as you open the app, the saved data should be displayed on the screen, then you can use react hook : useEffect like this

	useEffect(() => {
		getData();
	}, []);

Remove data from async storage

const removeValue = async () => {
		try {
			await AsyncStorage.removeItem("user");
			alert("Data Removed!");
		} catch (e) {
			
			console.log("error occured while removing data", e);
		}

	};

Clear all data from async storage

const clearAll = async () => {
		try {
			await AsyncStorage.clear();
			alert("All Data Cleared!");
		} catch (e) {
			// clear error
		}

		console.log("Done.");
	};

💻Complete Code

import React, { useEffect, useState } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";

export default function App() {
	const [input, setInput] = useState("");
	const [storedData, setStoredData] = useState("");

	useEffect(() => {
		getData();
	}, []);

	const saveData = async () => {
		try {
			await AsyncStorage.setItem("userInput", input);
			alert("Data Saved!");
		} catch (error) {
			console.log(error);
		}
	};

	const getData = async () => {
		try {
			const value = await AsyncStorage.getItem("userInput");
			if (value !== null) {
				setStoredData(value);
			}
		} catch (error) {
			console.log(error);
		}
	};

	const removeValue = async () => {
		try {
			await AsyncStorage.removeItem("userInput");
			alert("Data Removed!");
		} catch (e) {
			// remove error
			console.log("error occured while removing data", e);
		}

		console.log("Done.");
	};

	const clearAll = async () => {
		try {
			await AsyncStorage.clear();
			alert("All Data Cleared!");
		} catch (e) {
			// clear error
		}

		console.log("Done.");
	};

	return (
		<View style={styles.container}>
			<Text>Enter some text to save:</Text>
			<TextInput
				style={styles.input}
				value={input}
				onChangeText={setInput}
				placeholder="Type something..."
			/>
			<Button title="Save Data" onPress={saveData} />
			<Button title="Get Data" onPress={getData} />
			<Button title="Remove Data" onPress={removeValue} />
			<Button title="Clear All  Data" onPress={clearAll} />
			<Text>Stored Data: {storedData}</Text>
		</View>
	);
}

const styles = StyleSheet.create({
	container: {
		flex: 1,
		justifyContent: "center",
		alignItems: "center",
		backgroundColor: "#fff",
	},
	input: {
		borderWidth: 1,
		borderColor: "#ccc",
		padding: 10,
		width: "80%",
		marginVertical: 10,
	},
});

😄 More Real world use cases

1) Quiz App - Saving User Progress

Imagine a quiz app where users answer multiple-choice questions. If a user exits the app before completing the quiz, they should be able to continue from where they left off when they reopen it.

How AsyncStorage Helps:

This improves user experience by ensuring seamless progress tracking.

Showing add to a user after every 5 messages

2) Showing Ads After Every 5 Messages

In a chat app, you might want to show an ad after every 5 messages a user sends. Since app sessions can restart, you need a way to track the message count persistently.

How AsyncStorage Helps:

This ensures ads appear at the right intervals, even if the app restarts.