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.
- request or permission
- native module boundary
- result, error, or cleanup
Explain the React and native-platform behavior of AsyncStorage — Local Data Persistence
Core syntax or pattern
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
- request or permission
import 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.
- native module boundary
const 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.
- 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.
- request or permission
- native module boundary
- 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
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');
};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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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