Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI

License

NotificationsYou must be signed in to change notification settings

ammarahm-ed/react-native-mmkv-storage

Repository files navigation

License: MITAndroidiOS

Install the library

npm install react-native-mmkv-storage

For expo bare workflow

expo prebuild

Get Started with Documentation

What it is

This library aims to provide a fast & reliable solution for you data storage needs in react-native apps. It usesMMKV by Tencent under the hood on Android and iOS both that is used by their WeChat app(more than 1 Billion users). Unlike other storage solutions for React Native, this library lets you store any kind of data type, in any number of database instances, with or without encryption in a very fast and efficient way. Read about it on this blog post I wrote ondev.to

Learn how to build your own module with JSI on myblog

0.9.0 Breaking change

Works only with react native 0.71.0 and above. If you are on older version of react native, keep using 0.8.x.

Features

Written in C++ using JSI

Starting fromv0.5.0 the library has been rewritten in C++ on Android and iOS both. It employs React Native JSI making it the fastest storage option for React Native.

Simple and lightweight

(~ 50K Android/30K iOS) and even smaller when packaged.

Fast and Efficient (0.0002s Read/Write Speed)

MMKV uses mmap to keep memory synced with file, and protobuf to encode/decode values to achieve best performance.You can see the benchmarks here:Android &iOS

Reactive usinguseMMKVStorage &useIndex Hooks

Hooks let's the storage update your app when a change takes place in storage.

useMMKVStorage hook

Starting fromv0.5.5, thanks to the power of JSI, we now have our very ownuseMMKVStorage Hook. Think of it like a persisted state that will always write every change in storage and update your app UI instantly. It doesn't matter if you reload the app or restart it.

import{MMKVLoader,useMMKVStorage}from'react-native-mmkv-storage';conststorage=newMMKVLoader().initialize();constApp=()=>{const[user,setUser]=useMMKVStorage('user',storage,'robert');const[age,setAge]=useMMKVStorage('age',storage,24);return(<Viewstyle={styles.header}><Textstyle={styles.headerText}>        I am{user} and I am{age} years old.</Text></View>);};

Learn more aboutuseMMKVStorage hook it in thedocs.

useIndex hook

A hook that will take an array of keys and returns an array of values for those keys. This is supposed to work in combination with Transactions. When you have build your custom index, you will need an easy and quick way to load values for your index. useIndex hook actively listens to all read/write changes and updates the values accordingly.

constApp=()=>{// Get list of all post idsconstpostsIndex=useMMKVStorage("postsIndex",storage,[]);// ['post123','post234'];// Get the posts based on those ids.const[posts,update,remove]=useIndex(postsIndex,"object"storage);return<View><FlatListdata={posts}renderItem={...}></View>}

Learn more aboutuseIndex hook it in thedocs.

Lifecycle Control with Transaction Manager

Listen to a value's lifecycle and mutate it on the go. Transactions lets you register lifecycle functions with your storage instance such as Read, Write and Delete. This allows for a better and more managed control over the storage and also let's youbuild custom indexes with a few lines of code.

MMKV.transactions.register('object','beforewrite',({ key, value})=>{if(key.startsWith('post.')){// Call this only when the key has the post prefix.letindexForTag=MMKV.getArray(`${value.tag}-index`)||[];MMKV.setArray(indexForTag.push(key));}});

Learn more about how to use Transactions indocs

Multi-Process Support

MMKV supports concurrent read-read and read-write access between processes. This means that you can use MMKV for various extensions and widgets and your app.

Create unlimited Database instances

You can create many database instances. This helps greatly if you have separate logics/modules in the same app that use data differently, It also helps in better performance since each database instance is small instead of a single bulky database which makes things slower as it grows.

constuserStorage=newMMKVLoader().withEncryption().withInstanceID('userdata').initialize();constsettingsStorage=newMMKVLoader().withInstanceID('settings').initialize();

Full encryption support

The library supports full encryption (AES CFB-128) on Android and iOS. You can choose to store your encryption key securely for continuious usage. The library uses Keychain on iOS and Android Keystore on android (API 23 and above). Encrypting an instance is simple:

conststorage=newMMKVLoader().withEncryption()// Generates a random key and stores it securely in Keychain.initialize();

And that's it.

Simple indexer and data querying

For each database instance, there is one global key index and then there are indexes of each type of data. So querying is easy and fast.

Supports redux-persist

Support for redux persist is also added starting from v0.3.2.

Supports expo

You can use this library with expobare workflow.

Flipper plugin

Thanks topnthach95 Flipper plugin is finally here.https://github.com/pnthach95/flipper-plugin-react-native-mmkv-storage. It supports logging and manipulating storage values on the fly.

Consider supporting with a ⭐️star on GitHub

If you are using the library in one of your projects, consider supporting with a star. It takes a lot of time and effort to keep this maintained and address issues and bugs. Thank you.

Contact & Support

  • Create a GitHub issue for bug reports, feature requests, or questions
  • Follow@ammarahm-ed

I want to contribute

That is awesome news! There is a lot happening at a very fast pace in this library right now. Every little help is precious. You can contribute in many ways:

  • Suggest code improvements on native iOS and Android
  • If you have suggestion or idea you want to discuss, open an issue.
  • Open an issue if you want to make a pull request, and tell me what you want to improve or add so we can discuss
  • I am always open to new ideas

Special thanks to

License

This library is licensed under theMIT license.

Copyright © Ammar Ahmed (@ammarahm-ed)

Notesnook Logo

About

An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp