react native mmkv

2025-12-10 0 477

MMKV

The fastest key/value storage for React Native.

  • MMKV is an efficient, small mobile key-value storage framework developed by WeChat. See Tencent/MMKV for more information
  • reactnativemmkv 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.

Features

  • Get and set strings, booleans, numbers and ArrayBuffers
  • Fully synchronous calls, no async/await, no Promises, no Bridge.
  • Encryption support (secure storage)
  • Multiple instances support (separate user-data with global data)
  • Customizable storage location
  • High performance because everything is written in C++
  • ~30x faster than AsyncStorage
  • Uses JSI and C++ TurboModules instead of the \”old\” Bridge
  • iOS, Android and Web support
  • Easy to use React Hooks API

react-native-mmkv V3

Important

react-native-mmkv V3 is now a pure C++ TurboModule, and requires the new architecture to be enabled. (react-native 0.75+)

  • 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\”)
  • 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.
  • If you cannot use the new architecture yet, downgrade to react-native-mmkv 2.x.x for now.

Benchmark

StorageBenchmark compares popular storage libraries against each other by reading a value from storage for 1000 times:

MMKV vs other storage libraries: Reading a value from Storage 1000 times.
Measured in milliseconds on an iPhone 11 Pro, lower is better.

Installation

React Native

yarn add react-native-mmkv
cd ios && pod install

Expo

npx expo install react-native-mmkv
npx expo prebuild

Usage

Create a new instance

To 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.

Default

import { MMKV } from \'react-native-mmkv\'

export const storage = new MMKV()

This creates a new storage instance using the default MMKV storage ID (mmkv.default).

App Groups or Extensions

If 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.
See Configuring App Groups.

Customize

import { MMKV, Mode } from \'react-native-mmkv\'

export const storage = new MMKV({
  id: `user-${userId}-storage`,
  path: `${USER_DIRECTORY}/storage`,
  encryptionKey: \'hunter2\',
  mode: Mode.MULTI_PROCESS,
  readOnly: false
})

This 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.

The following values can be configured:

  • 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\')
  • 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 / Android)
  • 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 / Android)
  • 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).
  • 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.

Set

storage.set(\'user.name\', \'Marc\')
storage.set(\'user.age\', 21)
storage.set(\'is-mmkv-fast-asf\', true)

Get

const username = storage.getString(\'user.name\') // \'Marc\'
const age = storage.getNumber(\'user.age\') // 21
const isMmkvFastAsf = storage.getBoolean(\'is-mmkv-fast-asf\') // true

Hooks

const [username, setUsername] = useMMKVString(\'user.name\')
const [age, setAge] = useMMKVNumber(\'user.age\')
const [isMmkvFastAsf, setIsMmkvFastAf] = useMMKVBoolean(\'is-mmkv-fast-asf\')

Keys

// checking if a specific key exists
const hasUsername = storage.contains(\'user.name\')

// getting all keys
const keys = storage.getAllKeys() // [\'user.name\', \'user.age\', \'is-mmkv-fast-asf\']

// delete a specific key + value
storage.delete(\'user.name\')

// delete all keys
storage.clearAll()

Objects

const user = {
  username: \'Marc\',
  age: 21
}

// Serialize the object into a JSON string
storage.set(\'user\', JSON.stringify(user))

// Deserialize the JSON string into an object
const jsonUser = storage.getString(\'user\') // { \'username\': \'Marc\', \'age\': 21 }
const userObject = JSON.parse(jsonUser)

Encryption

// encrypt all data with a private key
storage.recrypt(\'hunter2\')

// remove encryption
storage.recrypt(undefined)

Buffers

const buffer = new ArrayBuffer(3)
const dataWriter = new Uint8Array(buffer)
dataWriter[0] = 1
dataWriter[1] = 100
dataWriter[2] = 255
storage.set(\'someToken\', buffer)

const buffer = storage.getBuffer(\'someToken\')
console.log(buffer) // [1, 100, 255]

Size

// get size of MMKV storage in bytes
const size = storage.size
if (size >= 4096) {
  // clean unused keys and clear memory cache
  storage.trim()
}

Testing with Jest or Vitest

A 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 for an example using Jest.

Documentation

  • Hooks
  • Value-change Listeners
  • Migrate from AsyncStorage
  • Using MMKV with redux-persist
  • Using MMKV with recoil
  • Using MMKV with mobx-persist-storage
  • Using MMKV with mobx-persist
  • Using MMKV with zustand persist-middleware
  • Using MMKV with jotai
  • Using MMKV with react-query
  • How is this library different from react-native-mmkv-storage?

LocalStorage and In-Memory Storage (Web)

If 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.

Limitations

  • react-native-mmkv V3 requires react-native 0.74 or higher.
  • react-native-mmkv V3 requires the new architecture/TurboModules to be enabled.
  • 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 or React DevTools.

Integrations

Flipper

Use flipper-plugin-react-native-mmkv to debug your MMKV storage using Flipper. You can also simply console.log an MMKV instance.

Reactotron

Use 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.

Community Discord

Join the Margelo Community Discord to chat about react-native-mmkv or other Margelo libraries.

Adopting at scale

react-native-mmkv is provided as is, I work on it in my free time.

If you\’re integrating react-native-mmkv in a production app, consider funding this project and contact me to receive premium enterprise support, help with issues, prioritize bugfixes, request features, help at integrating react-native-mmkv, and more.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

下载源码

通过命令行克隆项目:

git clone https://github.com/mrousavy/react-native-mmkv.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 react native mmkv https://www.zuozi.net/33558.html

sonar dotnet
上一篇: sonar dotnet
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务