{"id":13395324,"url":"https://github.com/mrousavy/react-native-mmkv","last_synced_at":"2025-05-12T05:20:54.847Z","repository":{"id":37534495,"uuid":"341816758","full_name":"mrousavy/react-native-mmkv","owner":"mrousavy","description":"⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!","archived":false,"fork":false,"pushed_at":"2025-04-28T04:51:01.000Z","size":35640,"stargazers_count":7042,"open_issues_count":51,"forks_count":274,"subscribers_count":31,"default_branch":"main","last_synced_at":"2025-05-12T02:39:07.894Z","etag":null,"topics":["android","cpp","database","fast","hacktoberfest","ios","jni","jsi","localstorage","mmkv","native-module","react","react-native","storage","turbomodule"],"latest_commit_sha":null,"homepage":"https://mrousavy.com","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrousavy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"mrousavy","patreon":null,"open_collective":null,"ko_fi":"mrousavy","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2021-02-24T07:33:00.000Z","updated_at":"2025-05-11T13:47:53.000Z","dependencies_parsed_at":"2022-07-14T11:01:34.038Z","dependency_job_id":"c17f20c8-04ae-4e34-948f-b159865397ed","html_url":"https://github.com/mrousavy/react-native-mmkv","commit_stats":{"total_commits":481,"total_committers":66,"mean_commits":7.287878787878788,"dds":0.5093555093555093,"last_synced_commit":"2ed261de716b0cfa6b092b92a76fbc6aa6247ad3"},"previous_names":[],"tags_count":91,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrousavy%2Freact-native-mmkv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrousavy%2Freact-native-mmkv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrousavy%2Freact-native-mmkv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrousavy%2Freact-native-mmkv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrousavy","download_url":"https://codeload.github.com/mrousavy/react-native-mmkv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253672696,"owners_count":21945480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","cpp","database","fast","hacktoberfest","ios","jni","jsi","localstorage","mmkv","native-module","react","react-native","storage","turbomodule"],"created_at":"2024-07-30T17:01:52.480Z","updated_at":"2025-05-12T05:20:54.815Z","avatar_url":"https://github.com/mrousavy.png","language":"C++","funding_links":["https://github.com/sponsors/mrousavy","https://ko-fi.com/mrousavy"],"categories":["Uncategorized","TypeScript","Storage:","C++","Storage","Storage \u0026 Database","Libraries","📦 技术选型"],"sub_categories":["Uncategorized","Graphics \u0026 Drawing","Others","核心库"],"readme":"\u003ca href=\"https://margelo.io\"\u003e\n  \u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"./package/img/banner-dark.png\" /\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"./package/img/banner-light.png\" /\u003e\n    \u003cimg alt=\"react-native-mmkv\" src=\"./package/img/banner-light.png\" /\u003e\n  \u003c/picture\u003e\n\u003c/a\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003eMMKV\u003c/h1\u003e\n  \u003ch3 align=\"center\"\u003eThe fastest key/value storage for React Native.\u003c/h3\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca align=\"center\" href=\"https://github.com/mrousavy?tab=followers\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/followers/mrousavy?label=Follow%20%40mrousavy\u0026style=social\" /\u003e\n  \u003c/a\u003e\n  \u003cbr/\u003e\n  \u003ca align=\"center\" href=\"https://twitter.com/mrousavy\"\u003e\n    \u003cimg src=\"https://img.shields.io/twitter/follow/mrousavy?label=Follow%20%40mrousavy\u0026style=social\" /\u003e\n  \u003c/a\u003e\n  \u003cbr /\u003e\n  \u003ca href=\"https://github.com/sponsors/mrousavy\"\u003e\n    \u003cimg align=\"right\" width=\"160\" alt=\"This library helped you? Consider sponsoring!\" src=\".github/funding-octocat.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\u003cbr/\u003e\n\n\n* **MMKV** is an efficient, small mobile key-value storage framework developed by WeChat. See [Tencent/MMKV](https://github.com/Tencent/MMKV) for more information\n* **react-native-mmkv** is a library that allows you to easily use **MMKV** inside your React Native app through fast and direct JS bindings to the native C++ library.\n\n## Features\n\n* **Get** and **set** strings, booleans, numbers and ArrayBuffers\n* **Fully synchronous** calls, no async/await, no Promises, no Bridge.\n* **Encryption** support (secure storage)\n* **Multiple instances** support (separate user-data with global data)\n* **Customizable storage location**\n* **High performance** because everything is **written in C++**\n* **~30x faster than AsyncStorage**\n* Uses [**JSI**](https://reactnative.dev/docs/the-new-architecture/landing-page#fast-javascriptnative-interfacing) and [**C++ TurboModules**](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules-xplat.md) instead of the \"old\" Bridge\n* **iOS**, **Android** and **Web** support\n* Easy to use **React Hooks** API\n\n## react-native-mmkv V3\n\n\u003e [!IMPORTANT]\n\u003e react-native-mmkv V3 is now a pure C++ TurboModule, and **requires the new architecture to be enabled**. (react-native 0.75+)\n\u003e - If you want to use react-native-mmkv 3.x.x, you need to enable the new architecture in your app (see [\"Enable the New Architecture for Apps\"](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md))\n\u003e - For React-Native 0.74.x, use react-native-mmkv 3.0.1. For React-Native 0.75.x and higher, use react-native-mmkv 3.0.2 or higher.\n\u003e - If you cannot use the new architecture yet, downgrade to react-native-mmkv 2.x.x for now.\n\n## Benchmark\n\n[StorageBenchmark](https://github.com/mrousavy/StorageBenchmark) compares popular storage libraries against each other by reading a value from storage for 1000 times:\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/mrousavy/StorageBenchmark\"\u003e\n    \u003cimg src=\"./package/img/benchmark_1000_get.png\" /\u003e\n  \u003c/a\u003e\n  \u003cp\u003e\n    \u003cb\u003eMMKV vs other storage libraries\u003c/b\u003e: Reading a value from Storage 1000 times. \u003cbr/\u003e\n    Measured in milliseconds on an iPhone 11 Pro, lower is better. \u003cbr/\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\n## Installation\n\n### React Native\n\n```sh\nyarn add react-native-mmkv\ncd ios \u0026\u0026 pod install\n```\n\n### Expo\n\n```sh\nnpx expo install react-native-mmkv\nnpx expo prebuild\n```\n\n## Usage\n\n### Create a new instance\n\nTo create a new instance of the MMKV storage, use the `MMKV` constructor. It is recommended that you re-use this instance throughout your entire app instead of creating a new instance each time, so `export` the `storage` object.\n\n#### Default\n\n```js\nimport { MMKV } from 'react-native-mmkv'\n\nexport const storage = new MMKV()\n```\n\nThis creates a new storage instance using the default MMKV storage ID (`mmkv.default`).\n\n#### App Groups or Extensions\n\nIf you want to share MMKV data between your app and other apps or app extensions in the same group, open `Info.plist` and create an `AppGroup` key with your app group's value. MMKV will then automatically store data inside the app group which can be read and written to from other apps or app extensions in the same group by making use of MMKV's multi processing mode.\nSee [Configuring App Groups](https://developer.apple.com/documentation/xcode/configuring-app-groups).\n\n#### Customize\n\n```js\nimport { MMKV, Mode } from 'react-native-mmkv'\n\nexport const storage = new MMKV({\n  id: `user-${userId}-storage`,\n  path: `${USER_DIRECTORY}/storage`,\n  encryptionKey: 'hunter2',\n  mode: Mode.MULTI_PROCESS,\n  readOnly: false\n})\n```\n\nThis creates a new storage instance using a custom MMKV storage ID. By using a custom storage ID, your storage is separated from the default MMKV storage of your app.\n\nThe following values can be configured:\n\n* `id`: The MMKV instance's ID. If you want to use multiple instances, use different IDs. For example, you can separate the global app's storage and a logged-in user's storage. (required if `path` or `encryptionKey` fields are specified, otherwise defaults to: `'mmkv.default'`)\n* `path`: The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#customize-location) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#customize-location))\n* `encryptionKey`: The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's/Android's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV. (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#encryption) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#encryption))\n* `mode`: The MMKV's process behaviour - when set to `MULTI_PROCESS`, the MMKV instance will assume data can be changed from the outside (e.g. App Clips, Extensions or App Groups).\n* `readOnly`: Whether this MMKV instance should be in read-only mode. This is typically more efficient and avoids unwanted writes to the data if not needed. Any call to `set(..)` will throw.\n\n### Set\n\n```js\nstorage.set('user.name', 'Marc')\nstorage.set('user.age', 21)\nstorage.set('is-mmkv-fast-asf', true)\n```\n\n### Get\n\n```js\nconst username = storage.getString('user.name') // 'Marc'\nconst age = storage.getNumber('user.age') // 21\nconst isMmkvFastAsf = storage.getBoolean('is-mmkv-fast-asf') // true\n```\n\n### Hooks\n\n```js\nconst [username, setUsername] = useMMKVString('user.name')\nconst [age, setAge] = useMMKVNumber('user.age')\nconst [isMmkvFastAsf, setIsMmkvFastAf] = useMMKVBoolean('is-mmkv-fast-asf')\n```\n\n### Keys\n\n```js\n// checking if a specific key exists\nconst hasUsername = storage.contains('user.name')\n\n// getting all keys\nconst keys = storage.getAllKeys() // ['user.name', 'user.age', 'is-mmkv-fast-asf']\n\n// delete a specific key + value\nstorage.delete('user.name')\n\n// delete all keys\nstorage.clearAll()\n```\n\n### Objects\n\n```js\nconst user = {\n  username: 'Marc',\n  age: 21\n}\n\n// Serialize the object into a JSON string\nstorage.set('user', JSON.stringify(user))\n\n// Deserialize the JSON string into an object\nconst jsonUser = storage.getString('user') // { 'username': 'Marc', 'age': 21 }\nconst userObject = JSON.parse(jsonUser)\n```\n\n### Encryption\n\n```js\n// encrypt all data with a private key\nstorage.recrypt('hunter2')\n\n// remove encryption\nstorage.recrypt(undefined)\n```\n\n### Buffers\n\n```js\nconst buffer = new ArrayBuffer(3)\nconst dataWriter = new Uint8Array(buffer)\ndataWriter[0] = 1\ndataWriter[1] = 100\ndataWriter[2] = 255\nstorage.set('someToken', buffer)\n\nconst buffer = storage.getBuffer('someToken')\nconsole.log(buffer) // [1, 100, 255]\n```\n\n### Size\n\n```js\n// get size of MMKV storage in bytes\nconst size = storage.size\nif (size \u003e= 4096) {\n  // clean unused keys and clear memory cache\n  storage.trim()\n}\n```\n\n## Testing with Jest or Vitest\n\nA mocked MMKV instance is automatically used when testing with Jest or Vitest, so you will be able to use `new MMKV()` as per normal in your tests. Refer to [package/example/test/MMKV.test.ts](package/example/test/MMKV.test.ts) for an example using Jest.\n\n## Documentation\n\n* [Hooks](./docs/HOOKS.md)\n* [Value-change Listeners](./docs/LISTENERS.md)\n* [Migrate from AsyncStorage](./docs/MIGRATE_FROM_ASYNC_STORAGE.md)\n* [Using MMKV with redux-persist](./docs/WRAPPER_REDUX.md)\n* [Using MMKV with recoil](./docs/WRAPPER_RECOIL.md)\n* [Using MMKV with mobx-persist-storage](./docs/WRAPPER_MOBX.md)\n* [Using MMKV with mobx-persist](./docs/WRAPPER_MOBXPERSIST.md)\n* [Using MMKV with zustand persist-middleware](./docs/WRAPPER_ZUSTAND_PERSIST_MIDDLEWARE.md)\n* [Using MMKV with jotai](./docs/WRAPPER_JOTAI.md)\n* [Using MMKV with react-query](./docs/WRAPPER_REACT_QUERY.md)\n* [How is this library different from **react-native-mmkv-storage**?](https://github.com/mrousavy/react-native-mmkv/issues/100#issuecomment-886477361)\n\n## LocalStorage and In-Memory Storage (Web)\n\nIf a user chooses to disable LocalStorage in their browser, the library will automatically provide a limited in-memory storage as an alternative. However, this in-memory storage won't persist data, and users may experience data loss if they refresh the page or close their browser. To optimize user experience, consider implementing a suitable solution within your app to address this scenario.\n\n## Limitations\n\n- react-native-mmkv V3 requires react-native 0.74 or higher.\n- react-native-mmkv V3 requires [the new architecture](https://reactnative.dev/docs/the-new-architecture/landing-page)/TurboModules to be enabled.\n- Since react-native-mmkv uses JSI for synchronous native method invocations, remote debugging (e.g. with Chrome) is no longer possible. Instead, you should use [Flipper](https://fbflipper.com) or [React DevTools](https://react.dev/learn/react-developer-tools).\n\n## Integrations\n\n### Flipper\n\nUse [flipper-plugin-react-native-mmkv](https://github.com/muchobien/flipper-plugin-react-native-mmkv) to debug your MMKV storage using Flipper. You can also simply `console.log` an MMKV instance.\n\n### Reactotron\n\nUse [reactotron-react-native-mmkv](https://www.npmjs.com/package/reactotron-react-native-mmkv) to automatically log writes to your MMKV storage using Reactotron. [See the docs for how to setup this plugin with Reactotron.](https://www.npmjs.com/package/reactotron-react-native-mmkv)\n\n## Community Discord\n\n[**Join the Margelo Community Discord**](https://margelo.com/discord) to chat about react-native-mmkv or other Margelo libraries.\n\n## Adopting at scale\n\nreact-native-mmkv is provided _as is_, I work on it in my free time.\n\nIf you're integrating react-native-mmkv in a production app, consider [funding this project](https://github.com/sponsors/mrousavy) and \u003ca href=\"mailto:me@mrousavy.com?subject=Adopting react-native-mmkv at scale\"\u003econtact me\u003c/a\u003e to receive premium enterprise support, help with issues, prioritize bugfixes, request features, help at integrating react-native-mmkv, and more.\n\n## Contributing\n\nSee the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrousavy%2Freact-native-mmkv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrousavy%2Freact-native-mmkv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrousavy%2Freact-native-mmkv/lists"}