What is react-native-mmkv?
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
Other packages similar to react-native-mmkv
react-native-async-storage
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
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
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
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 use MMKV inside your React Native applications.
Features
- Get and set strings, booleans and numbers
- Fully synchronous calls, no async/await, no Promises, no Bridge.
- High performance because everything is written in C++ (even the JS functions have C++ bodies!)
- ~30x faster than AsyncStorage
- Uses JSI instead of the "old" Bridge
Fun fact: since all the JS functions have C++ implementations, you can also directly call them in reanimated worklets, Vision Camera frame processors or custom threads using react-native-multithreading.
Benchmark
AsyncStorage vs MMKV: Reading a value from Storage 1000 times.
Measured in milliseconds on an iPhone 8, lower is better.
Installation
npm install react-native-mmkv
iOS
iOS installation is automatic, just run:
cd ios && pod install
Android
To correctly initialize MMKV on Android, please follow the Installation guide.
Usage
Set
import { MMKV } from 'react-native-mmkv';
MMKV.set('user.name', 'Marc')
MMKV.set('user.age', 20)
MMKV.set('is-mmkv-fast-asf', true)
Get
import { MMKV } from 'react-native-mmkv';
const username = MMKV.getString('user.name')
const age = MMKV.getNumber('user.age')
const isMmkvFastAsf = MMKV.getBoolean('is-mmkv-fast-asf')
Delete
import { MMKV } from 'react-native-mmkv';
MMKV.delete('user.name')
Get all keys
import { MMKV } from 'react-native-mmkv';
const keys = MMKV.getAllKeys()
Objects
import { MMKV } from 'react-native-mmkv';
const user = {
username: 'Marc',
age: 20
}
MMKV.set('user', JSON.stringify(user))
const jsonUser = MMKV.getString('user')
const userObject = JSON.parse(jsonUser)
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.
redux-persist
If you want to use MMKV with redux-persist, create the following storage
object:
import { MMKV } from "react-native-mmkv";
import { Storage } from "redux-persist";
export const storage: Storage = {
setItem: (key: string, value: string): Promise<boolean> => {
MMKV.set(key, value);
return Promise.resolve(true);
},
getItem: (key: string): Promise<string> => {
const value = MMKV.getString(key);
return Promise.resolve(value);
},
removeItem: (key: string): Promise<void> => {
MMKV.delete(key);
return Promise.resolve();
},
};
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT