Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rt2zz/unstated-persist
https://github.com/rt2zz/unstated-persist
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rt2zz/unstated-persist
- Owner: rt2zz
- Created: 2018-04-15T00:55:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T09:36:42.000Z (over 1 year ago)
- Last Synced: 2024-04-09T15:24:24.052Z (7 months ago)
- Language: JavaScript
- Size: 394 KB
- Stars: 19
- Watchers: 4
- Forks: 4
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Unstated Persist
unstated container w/ persistence
### Usage
Usage is simple, replace Container with PersistContainer and add persist config as a class property
```js
import { PersistContainer } from 'unstated-persist'
import localForage from 'localforage'type CounterState = {
count: number
};class CounterContainer extends PersistContainer {
persist = {
key: 'counter',
version: 1,
storage: localForage,
}
// ...
}
```### FAQ
#### Class inheritance are you crazy?
Well it works, and [its tiny and simple](https://github.com/rt2zz/unstated-persist/blob/master/packages/unstated-persist/src/unstated-persist.js). Risk of inheritance collision / confusion is minimal in this case.#### Migrations
In the future we will add redux-persist like migrations / transforms. For now, changing persist version will simply clobber stored state.#### PersistGate
An example of how PersistGate might be implemented [lives here](https://github.com/rt2zz/unstated-persist/tree/master/packages/unstated-persist-gate). However it is so simple, I expect in most cases components will their own gating. Something like:
```js
import { Subscribe } from 'unstated'
import { isBootstrapped } from 'unstated-persist'//...
{(...containers) => {
if (!containers.every(isBootstrapped)) return
return
}}```