Lesson 7 of 9
MMKV - High-Performance Synchronous Storage
MMKV is Tencent's high-performance key-value store used in WeChat. It's 30ร faster than AsyncStorage because it writes synchronously using memory-mapped files. Use it when AsyncStorage's async API creates awkward loading states in animations or startup logic.
MMKV Setup and Usage
# For Expo with bare workflow or RN CLI
npm install react-native-mmkv
cd ios && pod install
// ---- Basic usage ----
import { MMKV } from 'react-native-mmkv';
// Global store instance
export const storage = new MMKV({ id: 'app-storage' });
// Encrypted instance (for moderately sensitive data - not as secure as SecureStore)
export const secureStorage = new MMKV({
id: 'secure-storage',
encryptionKey: 'your-encryption-key', // use a device-specific key in production
});
// ---- Synchronous API - no await! ----
storage.set('theme', 'dark');
const theme = storage.getString('theme'); // string | undefined
storage.set('fontSize', 16);
const size = storage.getNumber('fontSize'); // number | undefined
storage.set('onboarded', true);
const done = storage.getBoolean('onboarded'); // boolean | undefined
storage.delete('theme');
storage.clearAll(); // nuclear option
// ---- Zustand + MMKV persistence (replaces AsyncStorage) ----
import { StateStorage } from 'zustand/middleware';
const mmkvStorage: StateStorage = {
getItem: (name) => storage.getString(name) ?? null,
setItem: (name, value) => storage.set(name, value),
removeItem: (name) => storage.delete(name),
};
// Use in persist middleware:
// storage: createJSONStorage(() => mmkvStorage)
// This makes Zustand hydration synchronous - no loading flashTip
Tip
Practice MMKV HighPerformance Synchronous Storage in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Direct memory-mapped synchronous key-value storage 30x faster than AsyncStorage
Practice Task
Note
Practice Task - (1) Write a working example of MMKV HighPerformance Synchronous Storage 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 MMKV HighPerformance Synchronous Storage 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.
Finished this lesson?