createReactNativeMmkvPersister
The createReactNativeMmkvPersister
function creates a ReactNativeMmkvPersister
object that can persist the Store
to a local react-native-mmkv
storage.
createReactNativeMmkvPersister(
store: Store | MergeableStore,
storage: MMKV,
storageName?: string,
onIgnoredError?: (error: any) => void,
): ReactNativeMmkvPersister
Type | Description | |
---|---|---|
store | Store | MergeableStore | The |
storage | MMKV | The MMKV storage instance. |
storageName? | string | The unique key to identify the storage location. |
onIgnoredError? | (error: any) => void | An optional handler for the errors that the |
returns | ReactNativeMmkvPersister | A reference to the new |
A ReactNativeMmkvPersister
supports both regular Store
and MergeableStore
objects.
As well as providing a reference to the Store
to persist, you must provide a storage
parameter which identifies the MMKV storage instance.
The third argument is a storageName
string that configures which key to use for the storage key.
Example
This example creates a ReactNativeMmkvPersister
object and persists the Store
to a MMKV storage instance as a JSON serialization into the my_tinybase
key. It makes a change to the storage directly and then reloads it back into the Store
.
import {MMKV} from 'react-native-mmkv';
import {createStore} from 'tinybase';
import {createReactNativeMmkvPersister} from 'tinybase/persisters/persister-react-native-mmkv';
const storage = new MMKV();
const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
const persister = createReactNativeMmkvPersister(
store,
storage,
'my_tinybase',
);
await persister.save();
// Store will be saved to the MMKV storage.
console.log(storage.getString('my_tinybase'));
// -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
await persister.destroy();
storage.delete('my_tinybase');
Since
v6.5.0