react-native-mmkv is a fast, small, and easy-to-use key-value storage library for React Native. It leverages Facebook's MMKV storage library to provide efficient and secure storage solutions for mobile applications.
What are react-native-mmkv's main functionalities?
Basic Key-Value Storage
This feature allows you to store and retrieve basic key-value pairs. The code sample demonstrates how to set and get a string value using MMKV.
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
// Set a value
MMKV.setString('username', 'john_doe');
// Get a value
const username = MMKV.getString('username');
console.log(username); // Output: john_doe
Object Storage
This feature allows you to store and retrieve objects. The code sample demonstrates how to set and get an object using MMKV.
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
const user = { name: 'John Doe', age: 30 };
// Set an object
MMKV.setMap('user', user);
// Get an object
const storedUser = MMKV.getMap('user');
console.log(storedUser); // Output: { name: 'John Doe', age: 30 }
Encryption
This feature allows you to store data securely using encryption. The code sample demonstrates how to set and get an encrypted string value using MMKV.
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().withEncryption().initialize();
// Set a value
MMKV.setString('secret', 'my_secret_value');
// Get a value
const secret = MMKV.getString('secret');
console.log(secret); // Output: my_secret_value
Multi-Instance Support
This feature allows you to create multiple instances of storage, which can be useful for separating different types of data. The code sample demonstrates how to set and get values from different instances using MMKV.
import MMKVStorage from 'react-native-mmkv-storage';
const userStorage = new MMKVStorage.Loader().withInstanceID('user').initialize();
const settingsStorage = new MMKVStorage.Loader().withInstanceID('settings').initialize();
// Set values in different instances
userStorage.setString('username', 'john_doe');
settingsStorage.setBool('darkMode', true);
// Get values from different instances
const username = userStorage.getString('username');
const darkMode = settingsStorage.getBool('darkMode');
console.log(username); // Output: john_doe
console.log(darkMode); // Output: true
react-native-async-storage is a simple, unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It is more widely used and has a larger community but lacks the performance and encryption features of react-native-mmkv.
react-native-sensitive-info provides secure storage for sensitive data, such as login credentials. It uses the device's secure storage mechanisms (Keychain on iOS and Keystore on Android). While it offers strong security, it may not be as fast as react-native-mmkv for general key-value storage.
redux-persist is a library used to persist and rehydrate a Redux store. It supports various storage backends, including AsyncStorage. While it is not a direct competitor, it can be used for state persistence in applications using Redux, offering more flexibility in terms of storage backends.
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
react-native-mmkv is a library that allows you to easily use MMKV inside your React Native applications. It provides fast and direct bindings to the native C++ library which are accessible through a simple JS API.
Features
Get and set strings, booleans and numbers
Fully synchronous calls, no async/await, no Promises, no Bridge.
Encryption support (secure storage)
Multiple instances support (separate user-data with global data)
Customize storage location
High performance because everything is written in C++
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.
This creates a new storage instance using the default MMKV storage ID (mmkv.default).
App Groups
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.
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)
// checking if a specific key existsconst hasUsername = storage.contains('user.name')
// getting all keysconst 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 objectconst 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)
A mocked MMKV instance is automatically used when testing with Jest, so you will be able to use new MMKV() as per normal in your tests. Refer to example/test/MMKV.test.ts for an example.
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
As the library uses JSI for synchronous native methods access, remote debugging (e.g. with Chrome) is no longer possible. Instead, you should use Flipper.
Flipper
Use flipper-plugin-react-native-mmkv to debug your MMKV storage using Flipper. You can also simply console.log an MMKV instance.
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.
The fastest key/value storage for React Native. ~30x faster than AsyncStorage! Works on Android, iOS and Web.
The npm package react-native-mmkv receives a total of 119,830 weekly downloads. As such, react-native-mmkv popularity was classified as popular.
We found that react-native-mmkv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 15 Feb 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.