Introduction
MMKV helps your app save and get data quickly. It works like a super-fast notebook for your app.
MMKV helps your app save and get data quickly. It works like a super-fast notebook for your app.
import { MMKV } from 'react-native-mmkv'; const storage = new MMKV(); // Save data storage.set('key', 'value'); // Get data const value = storage.getString('key');
Use set to save data with a key.
Use getString to get saved string data by key.
storage.set('username', 'Alice'); const name = storage.getString('username');
storage.set('isLoggedIn', 'true'); const loggedIn = storage.getString('isLoggedIn');
storage.delete('username');This app shows a saved name from MMKV storage. When you press the button, it saves 'Bob' and updates the display.
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; import { MMKV } from 'react-native-mmkv'; const storage = new MMKV(); export default function App() { const [name, setName] = useState(storage.getString('name') || 'No name saved'); const saveName = () => { storage.set('name', 'Bob'); setName(storage.getString('name')); }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Saved Name: {name}</Text> <Button title="Save Name" onPress={saveName} /> </View> ); }
MMKV stores data very fast compared to AsyncStorage.
It works well for small pieces of data like strings and booleans.
Remember to import and create an MMKV instance before using it.
MMKV is a fast way to save and get small data in React Native apps.
Use set to save and getString to read data.
It helps keep app data even after closing the app.
MMKV in a React Native app?set.save, store, and put are not valid MMKV methods.const MMKV = require('react-native-mmkv').MMKV;
const storage = new MMKV();
storage.set('name', 'Alice');
const result = storage.getString('name');
console.log(result);const storage = new MMKV();
storage.set('count', 5);
const value = storage.getString('count');
console.log(value);set to save the string 'dark' under 'theme'.getString('theme') and fallback to 'dark' if null or undefined using ??.Pass the test and this pattern moves unknown