Thuta Learning
React Native
AdvancedMobile DevelopmentIntermediate

AsyncStorage — Local Data Persistence

React NativeLesson 2125 min

What you'll walk away with

  • Explain the React and native-platform behavior of AsyncStorage — Local Data Persistence
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

AsyncStorage — Local Data Persistence, in plain language

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.

Problem → Why → Solution

The problem — why this concept matters

Trying to use AsyncStorage synchronously (without await) — it's Promise-based, so you need await/then

A common mistake

Storing sensitive data (passwords, tokens) in plain AsyncStorage without encrypting it

Core concept — understand how it works

Build AsyncStorage — Local Data Persistence as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Mental model
  1. request or permission
  2. native module boundary
  3. result, error, or cleanup

Explain the React and native-platform behavior of AsyncStorage — Local Data Persistence

Core syntax or pattern

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');
};

Build AsyncStorage — Local Data Persistence as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Read the code step by step

  1. request or permissionimport AsyncStorage from '@react-native-async-storage...

    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.

  2. native module boundaryconst json = await AsyncStorage.getItem('userPrefs');

    Build AsyncStorage — Local Data Persistence as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

  3. result, error, or cleanup};

    Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Execution flow
  1. request or permission
  2. native module boundary
  3. result, error, or cleanup

Success

Explain the React and native-platform behavior of AsyncStorage — Local Data Persistence

Failure

Trying to use AsyncStorage synchronously (without await) — it's Promise-based, so you need await/then

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.

Real-world usage

How to apply it in a real project

Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Mini challenge

Build it yourself

Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
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');
};

Expected: Explain the React and native-platform behavior of AsyncStorage — Local Data Persistence

Show hint

Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Quick quiz 1 / 3

What best describes the core idea of AsyncStorage — Local Data Persistence?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • request or permission → native module boundary → result, error, or cleanup
  • Build AsyncStorage — Local Data Persistence as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
  • Trying to use AsyncStorage synchronously (without await) — it's Promise-based, so you need await/then
  • Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

React Native Documentation — React Native

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
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original AsyncStorage — Local Data Persistence example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

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

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

AsyncStorage — Local Data Persistence | Thuta Learning