React Native Mmkv

margelo/react-native-skills/skills/react-native-mmkv

作者 margelo1e9e17be6f41c838db472d6a4b943fc26355a3beMIT收錄於 2026年10月9日更新於 2026年10月9日

Fast synchronous key-value storage for React Native via react-native-mmkv (Nitro-backed). Covers creating and configuring MMKV instances, reading/writing all value types (string, number, boolean, buffer), React hooks for reactive UI, value-change listeners, encryption, AsyncStorage migration, and state-management integrations (zustand, redux-persist, jotai, react-query). Also covers storage limits, multi-process mode, and the V4 upgrade from the class-based API.

AI 產生的概覽

在 React Native 專案中使用 react-native-mmkv 同步鍵值儲存的參考指南。

功能
此技能是給 AI 程式助理的參考指南,用於處理使用 react-native-mmkv(React Native 的同步鍵值儲存函式庫)的專案。它會把問題導向各參考檔案,涵蓋實例建立與設定、CRUD 操作、React hooks、值變更監聽器、加密、AsyncStorage 移轉、狀態管理整合、儲存限制以及 V4 升級。它提供真實的 API 名稱與用法模式,而非虛構的介面。
適用情境
在撰寫或審查使用 react-native-mmkv 儲存資料的程式碼時使用,例如保存 zustand 或 redux 狀態、加密儲存值,或從 AsyncStorage 移轉。它也適用於關於 MMKV 儲存限制、多程序模式或從 V3 升級到 V4 的問題。
執行需求
沒有指令碼,只有說明與參考文件。所描述的函式庫需要 react-native-mmkv 與 react-native-nitro-modules 作為對等相依套件,V4 需要 React Native 0.75+,安裝後需重新建置原生應用程式。

react-native-mmkv

A focused reference for AI coding assistants working in a project that uses react-native-mmkv. Answer using the real APIs from this library — not invented ones — by routing to the matching references/*.md file below.

Mental model

react-native-mmkv is a high-performance, synchronous key-value storage library for React Native, backed by Tencent's MMKV C++ engine via Nitro Modules (JSI). It is ~30x faster than AsyncStorage.

The three things that trip people up:

  1. Everything is synchronous. No await, no Promises. storage.getString('key') returns the value immediately. This is the whole point — MMKV memory-maps files and reads/writes in-process.
  2. It's a key-value store, not a database. All data is cached in memory. Keep total size under ~100 MB; for larger datasets use react-native-nitro-sqlite or WatermelonDB. There is no fixed storage limit — but memory warnings will fire if you go too large.
  3. V4 is a breaking rewrite. The constructor changed from new MMKV() to createMMKV(), .delete() became .remove(), and react-native-nitro-modules is now a required peer dependency. See the upgrade guide.

V4 requires React Native 0.75+. It works on both the New Architecture and the old architecture (Nitro is backwards compatible).

Routing table — problem to reference

Load the matching file from references/ before writing code. Each reference cites real APIs from the library.

User is asking about…Read
Creating an MMKV instance, configuration options (id, path, encryptionKey, mode, readOnly, compareBeforeSet), the createMMKV() factoryreferences/create-instance.md
Getting/setting values, all data types (string, number, boolean, buffer, objects via JSON), key management (contains, getAllKeys, remove, clearAll), size, trim, importAllFromreferences/crud-operations.md
React hooks (useMMKVString, useMMKVNumber, useMMKVBoolean, useMMKVBuffer, useMMKVObject, useMMKV, useMMKVListener, useMMKVKeys), reactive UIreferences/hooks.md
Listening for value changes outside of React, addOnValueChangedListener, cleaning up listenersreferences/listeners.md
Encryption (encrypt, decrypt, recrypt), AES-128 vs AES-256, encryptionKey configreferences/encryption.md
Migrating from AsyncStorage, data transfer pattern, completion flagreferences/migrate-from-async-storage.md
zustand persist middleware, redux-persist, jotai atomWithMMKV, react-query persister, MobXreferences/state-management.md
V4 upgrade, breaking changes from V3, new MMKV() → createMMKV(), .delete() → .remove()references/upgrade-v4.md
Storage size limits, memory warnings, when to use a database insteadreferences/limits-and-gotchas.md

If the question doesn't match any row, read references/create-instance.md first — most setup questions start there.

Installation

bash
npm install react-native-mmkv react-native-nitro-modulescd ios && pod install

Expo:

bash
npx expo install react-native-mmkv react-native-nitro-modulesnpx expo prebuild

After install, rebuild the app. No additional native wiring is required.

Quick reference — full MMKV instance API

ts
import { createMMKV, existsMMKV, deleteMMKV } from 'react-native-mmkv'
const storage = createMMKV({ id: 'my-store' })
// Properties (read-only)storage.id          // stringstorage.length      // number of keysstorage.byteSize    // total file size in bytesstorage.size        // (deprecated — use byteSize)storage.isReadOnly  // booleanstorage.isEncrypted // boolean
// Writestorage.set('key', 'string' | 42 | true | arrayBuffer)
// Readstorage.getString('key')   // string | undefinedstorage.getNumber('key')   // number | undefinedstorage.getBoolean('key')  // boolean | undefinedstorage.getBuffer('key')   // ArrayBuffer | undefined
// Keysstorage.contains('key')    // booleanstorage.getAllKeys()        // string[]storage.remove('key')      // boolean  (NOTE: was .delete() in V3)storage.clearAll()         // void
// Encryptionstorage.encrypt('password')storage.encrypt('password', 'AES-256')storage.decrypt()storage.recrypt('newPassword' | undefined)  // (deprecated — use encrypt()/decrypt())
// Maintenancestorage.trim()                       // free unused spacestorage.importAllFrom(otherStorage)  // returns count imported
// Listenersconst sub = storage.addOnValueChangedListener((key) => { ... })sub.remove()
// Static helpersexistsMMKV('my-store')   // booleandeleteMMKV('my-store')   // boolean

Testing

A mocked MMKV instance is automatically used when testing with Jest or Vitest. Use createMMKV() as normal in tests — no manual mocking needed.

Verifying the skill is loaded

Good test questions:

  • "How do I persist zustand state with MMKV?" → should produce a StateStorage adapter using storage.set, storage.getString, storage.remove — not AsyncStorage wrappers.
  • "How do I migrate from AsyncStorage to MMKV?" → should show the batch-copy pattern with a completion flag, not a drop-in replacement.
  • "What's the storage limit?" → should explain there's no fixed limit, but data is memory-mapped so keep it under ~100 MB; use SQLite for larger datasets.

References

FileDescription
create-instance.mdcreateMMKV() factory, all configuration options
crud-operations.mdSet/get all types, key management, objects, buffers
hooks.mdAll React hooks for reactive MMKV usage
listeners.mdaddOnValueChangedListener, cleanup
encryption.mdEncrypt/decrypt instances, AES options
migrate-from-async-storage.mdFull migration pattern from AsyncStorage
state-management.mdzustand, redux-persist, jotai, react-query, MobX
upgrade-v4.mdV3 → V4 breaking changes and migration
limits-and-gotchas.mdStorage limits, multi-process, logging, common pitfalls

Problem → Reference Mapping

ProblemReferenceAction
Don't know where to startcreate-instance.mdCreate instance with createMMKV()
Need reactive UI updateshooks.mdUse useMMKVString / useMMKVObject etc.
Encrypting sensitive dataencryption.mdCall storage.encrypt(key)
App uses AsyncStorage, want to switchmigrate-from-async-storage.mdBatch migrate then swap calls
Zustand/Redux/Jotai persistencestate-management.mdWire adapter for your state lib
Upgrading from V3 to V4upgrade-v4.mdnew MMKV() → createMMKV(), .delete() → .remove()
Storage growing too largelimits-and-gotchas.mdTrim or move to SQLite
Multi-process data sharinglimits-and-gotchas.mdSet mode: 'multi-process'

來源與署名

來源:margelo/react-native-skills位於skills/react-native-mmkv提交1e9e17b

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架