Module for Effector to sync stores with ReactNative AsyncStorage
npm install @effector-storage/react-native-async-storage
Adapter to persist [_store_] using React Native [AsyncStorage].
@effector-storage/react-native-async-storage has dependency on @react-native-async-storage/async-storage, so it will auto install it.
But depending on your platform and React Native version, you might want to install [AsyncStorage] manually, because it might require linking.
So, install [AsyncStorage], following documentation for your platform.
Depending on your package manager
``bashpnpmusing
↓
$ pnpm add effector-storage @effector-storage/react-native-async-storage
↓ ↓Usage
Import
persist function from '@effector-storage/react-native-async-storage' module, and it will just work:`javascript
import { persist } from '@effector-storage/react-native-async-storage'// persist store
$counter with key 'counter'
persist({ store: $counter, key: 'counter' })// if your storage has a name, you can omit
key field
persist({ store: $counter })
`⚠️ Note, that AsyncStorage is asynchronous, hence the name.
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
`javascript
import { persist } from '@effector-storage/react-native-async-storage'
`-
persist({ store, ...options }): Subscription
- persist({ source, target, ...options }): Subscription$3
- ... 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
`javascript
import { adapter } from '@effector-storage/react-native-async-storage'
`-
adapter(options?): StorageAdapter$3
-
serialize? (_(value: any) => string_): Custom serialize function. Default = JSON.stringify.
- deserialize? (_(value: string) => any_): Custom deserialize function. Default = JSON.parse.FAQ
$3
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)):`javascript
import { persist } from '@effector-storage/react-native-async-storage'const $date = createStore(new Date(), { name: 'date' })
persist({
store: $date,
serialize: (date) => String(date.getTime()),
deserialize: (timestamp) => new Date(Number(timestamp)),
})
``[asyncstorage]: https://react-native-async-storage.github.io/async-storage/
[_subscription_]: https://effector.dev/docs/glossary#subscription
[_store_]: https://effector.dev/docs/api/effector/store