0.1.2 • Published 5 years ago
mst-easy-storage v0.1.2
mst-easy-storage
A mobx-state-tree extension granting your models React Native's AsyncStorage powers of persistance.
Requirements
mobx-state-treereact-native@react-native-async-storage/async-storage
NOTE: @react-native-community/async-storage is DEPRECATED!
Installing
yarn add mst-easy-storageUsage
The following async actions are added:
import { types } from "mobx-state-tree"
import { withStorage } from "mst-easy-storage"
export const NiceThingsModel = types
.model("NiceThings")
.props({
unicorns: true,
dragons: true,
cake: true,
spiders: false,
nickleback: false,
})
.actions(self => ({
setSpiders(value: boolean) {
self.spiders = value
},
}))
.extend(withStorage({ key: "nice.things" })) // <-- 🎉Now you can load it:
async demo () {
// create your model as usual
const happy = NiceThingsModel.create()
// now load the data from async storage
await happy.load()
// and when you change something
happy.setSpiders(true)
// it will automatically save back to async storage!
}Options
withAsyncStorage() accepts an optional object as a parameter with these keys:
| key | type | what it does |
|---|---|---|
| key | string | The key to use when saving to AsyncStorage (default: the model type name) |
| autoSave | boolean | Should we automatically save when any values change on the model? (default: true) |
| mode | inclusive , exclusive, none | Filtering mode (default: none) |
| names | string[] | A list of property names that will be filtered as the mode specified. |
Contributing?
Yes plz!
Fork it, Clone it, Branch it, Yarn it
Build it, Test it, Push it, PR it