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.
Fun fact: since all the JS functions have C++ implementations, you can also directly call them in reanimated worklets
AsyncStorage vs MMKV: Reading a value from Storage 1000 times.
Measured in milliseconds on an iPhone 8, lower is better.
npm install react-native-mmkv
cd ios && pod install
import { MMKV } from 'react-native-mmkv';
MMKV.set('Marc', 'user.name')
MMKV.set(20, 'user.age')
MMKV.set(true, 'is-mmkv-fast-asf')
import { MMKV } from 'react-native-mmkv';
const username = MMKV.getString('user.name') // 'Marc'
const age = MMKV.getNumber('user.age') // 20
const isMmkvFastAsf = MMKV.getBoolean('is-mmkv-fast-asf') // true
import { MMKV } from 'react-native-mmkv';
MMKV.delete('user.name')
import { MMKV } from 'react-native-mmkv';
const keys = MMKV.getAllKeys() // ['user.name', 'user.age', 'is-mmkv-fast-asf']
import { MMKV } from 'react-native-mmkv';
const user = {
username: 'Marc',
age: 20
}
MMKV.set(JSON.stringify(user), 'user')
const jsonUser = MMKV.getString('user') // { 'username': 'Marc', 'age': 20 }
const userObject = JSON.parse(jsonUser)
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";
type StorageType = typeof MMKV & {
/**
* Redux Persist plugin for react-native-mmkv
*/
redux: Storage;
};
// Unfortunately redux-persist expects Promises, so we have to wrap our sync calls with Promise resolvers/rejecters
const storage: StorageType = {
redux: {
setItem: (key: string, value: string): Promise<boolean> => {
MMKV.set(value, key);
return Promise.resolve(true);
},
getItem: (key: string): Promise<string> =>
Promise.resolve(MMKV.getString(key)),
removeItem: (key: string): Promise<void> => {
MMKV.delete(key);
return Promise.resolve();
},
},
...MMKV,
};
export default storage;
See the contributing guide to learn how to contribute to the repository and the development workflow.
Author: mrousavy
Source Code: https://github.com/mrousavy/react-native-mmkv
#react-native #react #mobile-apps