An abstract persistence layer for your reactive state. Supports storage mocking, custom serializers/deserializers, migrations and storage subscriptions.
First of all, you need a persistence adapter. Every adapter is an operator which you can apply to an atom to persist its value. Most likely, the adapter you want is already implemented in withLocalStorage from @reatom/persist-web-storage. reatomPersist function can be used to create a custom persist adapter.
Creating an adapter
To create a custom persist adapter, implement the following interface:
See createMemStorage for an example of PersistStorage implementation.
Adapter options
Every adapter accepts the following set of options. Passing a string is identical to only passing the key option.
Testing
Every persist adapter has the storageAtom atom which allows you to mock an adapter’s storage when testing persisted atoms. createMemStorage function can be used to create such mocked storage.
SSR
A fully-featured SSR example with Next.js can be found here↗.
The example below shows how simple it is to implement an SSR adapter. To do so, create an in-memory storage with createMemStorage, use it to persist your atoms, and populate it before rendering the app.