Skip to main content

Lesson 5 of 9

AsyncStorage - Key-Value Persistence

AsyncStorage is React Native's built-in key-value store - the equivalent of localStorage. It's async, survives app restarts, and works cross-platform. It is NOT encrypted; never store tokens or sensitive data here.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

AsyncStorage Patterns

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

// ---- Basic get/set/remove ----
// Store
await AsyncStorage.setItem('theme', 'dark');
await AsyncStorage.setItem('user_prefs', JSON.stringify({ fontSize: 16, notifications: true }));

// Read
const theme = await AsyncStorage.getItem('theme'); // string | null
const prefs = await AsyncStorage.getItem('user_prefs');
const parsed = prefs ? JSON.parse(prefs) : null;

// Remove
await AsyncStorage.removeItem('theme');

// Batch operations (atomic, single native call - more efficient)
await AsyncStorage.multiSet([
  ['key1', 'value1'],
  ['key2', JSON.stringify({ complex: true })],
]);
const values = await AsyncStorage.multiGet(['key1', 'key2']);
// Returns: [['key1', 'value1'], ['key2', '{"complex":true}']]

// ---- Storage migration pattern ----
const STORAGE_VERSION_KEY = 'storage_version';
const CURRENT_VERSION = 2;

async function migrateStorageIfNeeded() {
  const raw = await AsyncStorage.getItem(STORAGE_VERSION_KEY);
  const version = raw ? parseInt(raw) : 0;

  if (version < 1) {
    // v0 โ†’ v1: rename 'userToken' to 'access_token'
    const old = await AsyncStorage.getItem('userToken');
    if (old) {
      await AsyncStorage.setItem('access_token', old);
      await AsyncStorage.removeItem('userToken');
    }
  }

  if (version < 2) {
    // v1 โ†’ v2: clear outdated cache
    await AsyncStorage.removeItem('feed_cache');
  }

  await AsyncStorage.setItem(STORAGE_VERSION_KEY, String(CURRENT_VERSION));
}

// ---- What NOT to store in AsyncStorage ----
// โŒ Passwords, access tokens, API keys โ†’ use expo-secure-store (encrypted on device keychain)
// โŒ Images, large binary files โ†’ use FileSystem API
// โœ… Theme preference, font size, onboarding completion, non-sensitive cache

Tip

Tip

Practice AsyncStorage KeyValue Persistence in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Asynchronous string storage for user settings and local offline preferences

Practice Task

Note

Practice Task - (1) Write a working example of AsyncStorage KeyValue Persistence from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with AsyncStorage KeyValue Persistence is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done