TechnicalMedium React Native

How do you store data locally in a React Native app?

Asked for Mobile Developer

Why they ask this: Offline support and secure token storage come up in nearly every mobile project.

Answer

Choose storage by what you are saving:

  • AsyncStorage (@react-native-async-storage/async-storage): simple, unencrypted key-value storage for small, non-sensitive data such as settings or onboarding flags.
  • MMKV (react-native-mmkv): a much faster key-value store, also synchronous.
  • Secure storage for tokens and secrets: expo-secure-store or react-native-keychain, which use the iOS Keychain and the Android Keystore.
  • A database for larger, structured or offline-first data: SQLite (expo-sqlite), WatermelonDB or Realm.
js
import * as SecureStore from 'expo-secure-store';

await SecureStore.setItemAsync('auth_token', token);
const saved = await SecureStore.getItemAsync('auth_token');

Never put access tokens in AsyncStorage, and never ship API secrets inside the app bundle, because the bundle can be unpacked.

Can you explain this in your own words?

Back to question bank