React Native MMKV Storage

4.3.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an MMKV storage instance, store various data types (string, number, boolean, JSON objects), retrieve them, and delete specific keys. It also includes an example of setting up an encrypted storage instance.

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'));

view raw JSON →