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-storeorreact-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?
Related questions
- EasyHow does React Native work, and how is it different from Flutter and fully native development?
- EasyWhen should you use FlatList instead of ScrollView in React Native?
- EasyHow do you write platform-specific code in React Native?
- MediumHow do you find and fix performance problems in a React Native app?