# mst-async-storage-upgraded

> Brings AsyncStorage support to mobx-state-tree for React Native projects.

Latest version **0.2.1** (published 2019-09-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install mst-async-storage-upgraded
pnpm add mst-async-storage-upgraded
yarn add mst-async-storage-upgraded
bun add mst-async-storage-upgraded
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-09-18 |
| First published | 2019-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 66.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | Steve Kellock |
| Maintainers | gailardiagalane |

## Links

- npm: https://www.npmjs.com/package/mst-async-storage-upgraded
- Repository: https://github.com/skellock/mst-async-storage
- Issues: https://github.com/skellock/mst-async-storage/issues
- npm.io page: https://npm.io/package/mst-async-storage-upgraded

## Recent versions

- 0.2.1 (latest) — 2019-09-18
- 0.2.0 — 2019-09-18

## README

# mst-async-storage

A [mobx-state-tree](https://github.com/mobxjs/mobx-state-tree) extension granting your models React Native's `AsyncStorage` powers of persistance.

# Requirements

- `mobx-state-tree` 2.x or 3.x
- `react-native` >= 0.56
- `@react-native-community/async-storage` >= 1.x.x

# Installing

```sh
yarn add mst-async-storage-upgraded
```

# Usage

The following async actions are added:

```ts
import { types } from "mobx-state-tree"
import { withAsyncStorage } from "mst-async-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(withAsyncStorage({ key: "nice.things" })) // <-- 🎉
```

Now you can load it:

```js
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`) |
| only     | string, string[] | will only include the keys with these names                                         |
| except   | string, string[] | will omit keys with these names                                                     |

# Contributing?

Yes plz!

```
Fork it, Clone it, Branch it, Yarn it
Build it, Test it, Push it, PR it
```

To run the tests, I like to open two shells `yarn test:compile:watch` and `yarn test --watch`. If you run `ava` manually, make sure to add `-s` to run the tests serially because I fail mocking (because the mocks are shared state and bleed into each other).

---
_Source: https://npm.io/package/mst-async-storage-upgraded · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
