React Native MMKV Storage
react-native-mmkv is a high-performance, persistent key-value storage solution for React Native applications. It leverages Tencent's MMKV library, which is written in C++ and optimized for speed, providing significantly faster read/write operations compared to AsyncStorage. The current stable version is 4.3.1. The library maintains a rapid release cadence, with multiple minor and patch releases occurring monthly, indicating active development and responsiveness to bug fixes and feature enhancements. Key differentiators include its raw speed, native implementation via Nitro, and seamless integration with both old and new React Native architectures, supporting various data types efficiently. Version 4.x introduced a complete rewrite using Nitro and now integrates `MMKVCore` through native dependency managers like CocoaPods and Gradle, simplifying its native setup.
Common errors
-
Invariant Violation: A `TurboReactPackage` could not be found for 'react-native-mmkv'.
cause The `TurboReactPackage` implementation was incorrect or missing a necessary fallback, particularly on specific React Native versions or architectures.fixUpgrade `react-native-mmkv` to version 4.3.1 or higher, which includes a fix to replace `TurboReactPackage` with `BaseReactPackage` for broader compatibility. -
Native module 'MMKV' was not found. Are you sure you've linked all of your native dependencies?
cause The native module for MMKV is not correctly linked or built into your application. This can happen due to caching issues, incorrect `Podfile` configuration, or outdated build artifacts.fixEnsure you have run `npx pod-install` (iOS) or `cd android && ./gradlew clean` then `cd ..` and rebuild your app. For v4, also verify that `react-native-nitro-modules` is correctly installed and linked. -
Error: MMKV instance 'my-id' already exists!
cause You are attempting to create an `MMKV` instance with an `id` that has already been initialized in the current application lifecycle.fixEnsure that each `MMKV` instance you create has a unique `id`. If you intend to use the same storage, reuse the existing instance instead of creating a new one with the same `id`. -
Could not find 'react-native-nitro-modules' for platform 'android'. Make sure to add it to your dependencies and link it correctly.
cause The `react-native-nitro-modules` peer dependency, essential for `react-native-mmkv` v4+, is either not installed, not correctly linked, or its version is incompatible.fixInstall/update `react-native-nitro-modules` (`npm install react-native-nitro-modules@latest`) and ensure your `android/build.gradle` and `app/build.gradle` configurations are up-to-date according to the `react-native-mmkv` and `nitro` documentation.
Warnings
- breaking Version 4.0.0 is a complete rewrite to Nitro, changing underlying architecture and dependencies. Direct migration from v3.x to v4.x may require significant code and configuration adjustments.
- breaking Versions 4.2.0 and higher require `react-native-nitro-modules` version 0.35.0 or higher. Older versions of Nitro Modules will cause build failures or runtime errors.
- gotcha In `v4.0.0-beta.12`, the `MMKVCore` pod was integrated directly, removing the need to manually add `pod 'MMKVCore'` to your `Podfile`. Keeping it will cause build errors.
- gotcha Using `set(ArrayBuffer)` on Web platforms was bugged in previous versions, leading to potential data corruption or unexpected behavior.
- gotcha Certain Android builds experienced issues with duplicate `libNitroModules.so` libraries, leading to build failures or runtime crashes.
Install
-
npm install react-native-mmkv -
yarn add react-native-mmkv -
pnpm add react-native-mmkv
Imports
- MMKV
const MMKV = require('react-native-mmkv')import { MMKV } from 'react-native-mmkv' - useMMKVStorage
import { useMMKVStorage } from 'react-native-mmkv' - MMKVLoader
import { MMKVLoader } from 'react-native-mmkv'
Quickstart
import { MMKV } from 'react-native-mmkv';
const storage = new MMKV();
// Set values
storage.set('user.name', 'John Doe');
storage.set('user.age', 30);
storage.set('isLoggedIn', true);
storage.set('lastLoginTimestamp', Date.now());
const settings = { theme: 'dark', notifications: true };
storage.set('appSettings', JSON.stringify(settings));
// Get values
const userName = storage.getString('user.name');
const userAge = storage.getNumber('user.age');
const isLoggedIn = storage.getBoolean('isLoggedIn');
const lastLogin = storage.getNumber('lastLoginTimestamp');
const appSettingsString = storage.getString('appSettings');
const appSettings = appSettingsString ? JSON.parse(appSettingsString) : {};
console.log(`User: ${userName}, Age: ${userAge}, Logged In: ${isLoggedIn}`);
console.log(`App Settings: ${JSON.stringify(appSettings)}`);
// Delete values
storage.delete('user.age');
console.log('User age after deletion:', storage.getNumber('user.age')); // Should be undefined or default
// Clear all data
// storage.clearAll();
// console.log('All data cleared:', storage.getAllKeys());
// Example with encrypted storage (requires a separate 'id')
const encryptedStorage = new MMKV({ id: 'my-encrypted-storage', encryptionKey: process.env.MMKV_ENCRYPTION_KEY ?? 'my-secret-key' });
encryptedStorage.set('secretData', 'This is a secret!');
console.log('Encrypted data:', encryptedStorage.getString('secretData'));