Thuta Learning
AdvancedMobile Developmentintermediate

AsyncStorage — Local Data Persistence

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand AsyncStorage — Local Data Persistence without the intimidation factor
  • Write the code yourself and run it on Expo Go
  • Apply this concept immediately in a real app project

Let's think about it this way for a second

AsyncStorage is key-value storage that persists data on the device (it survives even after the app closes) — conceptually similar to a web browser's localStorage, but it's an asynchronous (Promise-based) API. Save with setItem(key, value), read with getItem(key), and delete with removeItem(key) — since values can only be stored as strings, you need to convert objects/arrays with JSON.stringify/JSON.parse.

Let's connect this to a real-world scenario

Store a user preference (theme, language) or a login token in AsyncStorage, and when the app closes and reopens, you don't need to show the login screen again — if a token exists, you can navigate straight to the Home screen (the auto-login pattern). Sensitive data (passwords, payment info) should never be stored as plain text in AsyncStorage — use encrypted storage (expo-secure-store) instead (remember the secret management concept from the Cybersecurity tutorial).

Code Example

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

// Save
const saveUserPrefs = async (prefs) => {
  await AsyncStorage.setItem('userPrefs', JSON.stringify(prefs));
};

// Load
const loadUserPrefs = async () => {
  const json = await AsyncStorage.getItem('userPrefs');
  return json ? JSON.parse(json) : null;
};

// Remove
const clearUserPrefs = async () => {
  await AsyncStorage.removeItem('userPrefs');
};
You should see
Close and reopen the app, and you can read back the userPrefs that were saved in AsyncStorage.

Try it in 5 minutes

Write two functions yourself that save/load a theme preference (light/dark) to and from AsyncStorage.

A quick word of caution

Data in AsyncStorage can be read by anyone with device root access (a rooted/jailbroken device) — for truly sensitive data (payment credentials), use only expo-secure-store or the platform's native secure storage.

Easy traps

  • Trying to use AsyncStorage synchronously (without await) — it's Promise-based, so you need await/then
  • Storing sensitive data (passwords, tokens) in plain AsyncStorage without encrypting it

Now try it yourself

Write two functions yourself that save/load a theme preference (light/dark) to and from AsyncStorage.

You'll know it worked when: Close and reopen the app, and you can read back the userPrefs that were saved in AsyncStorage.

AsyncStorage — Local Data Persistence | Thuta Learning