Adds polyfill for localstorage using AsyncStorage or MMKV
npm install react-native-localstorage-polyfillChoose one (or both, if migrating):
AsyncStorage (default):
npm install @react-native-async-storage/async-storageMMKV (faster, synchronous):
npm install react-native-mmkvMMKV v4+ uses Nitro modules under the hood. Make sure
react-native-nitro-modulesis installed (it comes as a peer dependency ofreact-native-mmkvv4). Both MMKV v2/v3 and v4 are supported.
npx pod-installreact-native link @react-native-async-storage/async-storageSimply import react-native-localstorage-polyfill in the root of your app:
import 'react-native-localstorage-polyfill';localStorage.setItem('key', 'value');
localStorage.getItem('key');import 'react-native-localstorage-polyfill';
import { configure } from 'react-native-localstorage-polyfill';
import { MMKVBackend } from 'react-native-localstorage-polyfill/mmkv';
configure({ backend: new MMKVBackend() });
await localStorage.init();
localStorage.setItem('key', 'value');
localStorage.getItem('key');You can pass your own MMKV instance for encryption or custom storage IDs:
import { createMMKV } from 'react-native-mmkv';
import { configure } from 'react-native-localstorage-polyfill';
import { MMKVBackend } from 'react-native-localstorage-polyfill/mmkv';
const mmkv = createMMKV({
id: 'my-app-storage',
encryptionKey: 'my-secret-key',
});
configure({ backend: new MMKVBackend(mmkv) });Note: MMKV v2/v3 used
new MMKV(...), while v4+ usescreateMMKV(...). This library supports both — just pass the instance you create.
If you're already using this library with AsyncStorage and want to switch to MMKV, use the built-in migration utility:
import { configure, migrateStorage, AsyncStorageBackend } from 'react-native-localstorage-polyfill';
import { MMKVBackend } from 'react-native-localstorage-polyfill/mmkv';
async function setupStorage() {
const mmkvBackend = new MMKVBackend();
// Migrate all data from AsyncStorage to MMKV (run once)
await migrateStorage({
from: new AsyncStorageBackend(),
to: mmkvBackend,
deleteAfterMigration: true, // clears AsyncStorage after migration
});
// Use MMKV going forward
configure({ backend: mmkvBackend });
await localStorage.init();
}After confirming the migration is successful, you can optionally uninstall @react-native-async-storage/async-storage.
Set the storage backend. Must be called before localStorage.init().
Migrates all data from one backend to another. Returns { migratedKeys: number }.
Storage backend using @react-native-async-storage/async-storage.
Storage backend using react-native-mmkv. Import from react-native-localstorage-polyfill/mmkv.
Constructor: new MMKVBackend(mmkvInstance?) — optionally accepts a pre-created MMKV instance (from createMMKV() in v4+ or new MMKV() in v2/v3).
Interface for implementing custom storage backends:
interface StorageBackend {
init(): Promise<[string, any][]>;
setItem(key: string, value: string): void | Promise<void>;
removeItem(key: string): void | Promise<void>;
clear(): void | Promise<void>;
}See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT