0
0
React Nativemobile~5 mins

MMKV for fast storage in React Native

Choose your learning style9 modes available
Introduction

MMKV helps your app save and get data quickly. It works like a super-fast notebook for your app.

You want to save user settings like theme or language.
You need to remember if a user is logged in or not.
You want to store small data that your app uses often.
You want faster data access than regular storage.
You want to keep data even if the app closes.
Syntax
React Native
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.

Examples
Saves 'Alice' as username and gets it back.
React Native
storage.set('username', 'Alice');
const name = storage.getString('username');
Saves login status as a string and retrieves it.
React Native
storage.set('isLoggedIn', 'true');
const loggedIn = storage.getString('isLoggedIn');
Deletes the saved username from storage.
React Native
storage.delete('username');
Sample App

This app shows a saved name from MMKV storage. When you press the button, it saves 'Bob' and updates the display.

React Native
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>
  );
}
OutputSuccess
Important Notes

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.

Summary

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.