0.1.2 • Published 3 years ago

mst-easy-storage v0.1.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

mst-easy-storage

A mobx-state-tree extension granting your models React Native's AsyncStorage powers of persistance.

Requirements

  • mobx-state-tree
  • react-native
  • @react-native-async-storage/async-storage

NOTE: @react-native-community/async-storage is DEPRECATED!

Installing

yarn add mst-easy-storage

Usage

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:

keytypewhat it does
keystringThe key to use when saving to AsyncStorage (default: the model type name)
autoSavebooleanShould we automatically save when any values change on the model? (default: true)
modeinclusive , exclusive, noneFiltering mode (default: none)
namesstring[]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
0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago