Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Data, Storage and Networking

Storing Data on the Device

Persist settings, drafts and tokens in an Expo app: AsyncStorage for ordinary data, expo-secure-store for secrets, JSON helpers, key naming and migration.

A phone app is closed and reopened all the time, and the operating system may kill it in the background. Anything you keep only in React state disappears. To remember a user's theme, a half-written note or a login token, you save it on the device.

Expo gives you two small key-value stores that cover most needs. AsyncStorage holds ordinary data. expo-secure-store holds secrets inside the iOS Keychain or the Android Keystore. Choosing the right one is the main decision of this lesson.

What runs on this site. Storage is native, so the real calls are read-only. The runnable examples use an in-memory stand-in that has the same async API, so the patterns (JSON helpers, versioned keys, migrations) are real.

AsyncStorage

Install it with npx expo install @react-native-async-storage/async-storage. Every value is a string, and every call is asynchronous.

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

await AsyncStorage.setItem('theme', 'dark');
const theme = await AsyncStorage.getItem('theme'); // 'dark', or null if missing
await AsyncStorage.removeItem('theme');

To store objects, serialize them yourself:

JSX
await AsyncStorage.setItem('settings', JSON.stringify({ sound: true, size: 16 }));
const raw = await AsyncStorage.getItem('settings');
const settings = raw ? JSON.parse(raw) : { sound: true, size: 16 };

Because this pattern repeats everywhere, wrap it once.

Node.js JSON storage helpers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Always give get a fallback and guard JSON.parse; a corrupted value should never crash the app.

What AsyncStorage is good and bad at

Good for: theme, language, onboarding-done flag, small drafts, cached API responses. Not good for:

  • Secrets. It is not encrypted. Anyone with a rooted phone or a backup can read it.
  • Large or relational data. Thousands of records, searching and sorting belong in SQLite (next lessons).
  • Fast, synchronous reads. Every call is async, so you cannot read it during the first render.

expo-secure-store

Terminal
npx expo install expo-secure-store
JSX
import * as SecureStore from 'expo-secure-store';

await SecureStore.setItemAsync('accessToken', token);
const stored = await SecureStore.getItemAsync('accessToken');
await SecureStore.deleteItemAsync('accessToken');

Values are encrypted by the OS. Rules to remember:

  • Keys may contain only letters, digits, ., - and _.
  • Values are strings, and the size is limited (about 2 KB is a safe maximum on iOS in older versions), so store tokens, not documents.
  • On Android, data is removed when the app is uninstalled. On iOS the Keychain may survive an uninstall unless you handle it.
  • It is not available on the web, where you need a different approach.

You can ask the store to require biometrics with the requireAuthentication: true option, so reading a value triggers Face ID or a fingerprint.

Deciding which store

flowchart TD
  A["Need to save something"] --> B{"Is it a secret?"}
  B -- "yes" --> S["SecureStore"]
  B -- "no" --> C{"Large, or needs queries?"}
  C -- "yes" --> Q["expo-sqlite"]
  C -- "no" --> D["AsyncStorage"]

Secret first, size second.

Naming and versioning keys

Prefix keys with the feature and a version: settings:v2, drafts:v1. When you change the shape of what you store, bump the version and migrate old data instead of crashing on it.

Node.js Migrating stored data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Loading stored data at startup

Because reads are async, show a splash screen until your data is ready. With Expo you keep it visible using expo-splash-screen:

JSX
import * as SplashScreen from 'expo-splash-screen';
SplashScreen.preventAutoHideAsync();

// after loading settings:
SplashScreen.hideAsync();

Or render a simple loading view while useEffect reads the data, then render the real UI.

Clearing data on logout

When a user logs out, delete the token from SecureStore and remove any cached personal data. Forgetting this is a real privacy bug on shared devices.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should an access token be stored?

    SecureStore uses the Keychain or Keystore. AsyncStorage is not encrypted.
  2. 2. What type of value can AsyncStorage.setItem store?

    Values must be strings, so objects are saved with JSON.stringify.
  3. 3. Why use a key like settings:v2?

    A version in the key makes migrations explicit and safe.
  4. 4. Which data fits SQLite better than AsyncStorage?

    SQLite supports queries and indexes, key-value stores do not.

Summary

  • AsyncStorage is a simple async string store for non-sensitive data; wrap it with JSON helpers and fallbacks.
  • SecureStore keeps small secrets encrypted by the OS; its key characters and sizes are limited.
  • Version your keys and migrate old data.
  • Large or queryable data belongs in SQLite.
  • Clear tokens and personal caches on logout.

Next: SQLite databases with expo-sqlite