React Native EncryptedStorage adapter
![bundlejs](https://deno.bundlejs.com/badge?q=@effector-storage/react-native-encrypted-storage&treeshake=%5B%7Bpersist%7D%5D&config=%7B%22esbuild%22:%7B%22external%22:%5B%22effector%22,%22react-native-encrypted-storage%22%5D%7D%7D)
Adapter to persist store using React Native EncryptedStorage.
Preconditions
@effector-storage/react-native-encrypted-storage
has dependency on react-native-encrypted-storage
, so it will auto install it.
But depending on your platform and React Native version, you might want to install EncryptedStorage manually, because it might require linking.
So, install EncryptedStorage, following documentation for your platform.
Install
Depending on your package manager
$ pnpm add effector-storage @effector-storage/react-native-encrypted-storage
$ yarn add effector-storage @effector-storage/react-native-encrypted-storage
$ npm install --save effector-storage @effector-storage/react-native-encrypted-storage
Usage
Import persist
function from '@effector-storage/react-native-encrypted-storage'
module, and it will just work:
import { persist } from '@effector-storage/react-native-encrypted-storage'
persist({ store: $counter, key: 'counter' })
persist({ store: $counter })
⚠️ Note, that EncryptedStorage is asynchronous.
Two (or more) different stores, persisted with the same key, will be synchronized (synchronously!), even if not connected with each other directly — each store will receive updates from another one.
Formulae
import { persist } from '@effector-storage/react-native-encrypted-storage'
persist({ store, ...options }): Subscription
persist({ source, target, ...options }): Subscription
Options
- ... all the common options from
effector-storage
's persist
function. serialize
? ((value: any) => string): Custom serialize function. Default = JSON.stringify
.deserialize
? ((value: string) => any): Custom deserialize function. Default = JSON.parse
.
Adapter
import { adapter } from '@effector-storage/react-native-encrypted-storage'
adapter(options?): StorageAdapter
Options
serialize
? ((value: any) => string): Custom serialize function. Default = JSON.stringify
.deserialize
? ((value: string) => any): Custom deserialize function. Default = JSON.parse
.
FAQ
How do I use custom serialization / deserialization?
Options serialize
and deserialize
are got you covered. But make sure, that serialization is stable, meaning, that deserialize(serialize(object))
is equal to object
(or serialize(deserialize(serialize(object))) === serialize(object)
):
import { persist } from '@effector-storage/react-native-encrypted-storage'
const $date = createStore(new Date(), { name: 'date' })
persist({
store: $date,
serialize: (date) => String(date.getTime()),
deserialize: (timestamp) => new Date(Number(timestamp)),
})