1.0.3 • Published 4 years ago

mobx-state-tree-localstorage v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

mobx-state-tree-localstorage

npm version CircleCI

Persist your mobx-state-tree stores in localStorage

What is this?

mobx-state-tree-localstorage is a helper package, which allows you to easily persist your mob-state-tree stores in localStorage.

Setup

1. Installation

yarn add mobx-state-tree-localstorage

You need to also have mobx and mobx-state-tree installed.

2. Usage

Instead of calling model.create(), pass it into the persistInLocalStorage function, which will persist the tree in localStorage, and also return the created model:

// Change this
const store = Store.create({
    todos: [
        {
            title: "Get coffee"
        }
    ]
})

// To this
const store = persistInLocalStorage({
  tree: Store,
  id: '@myapp/TodoStore',
  initialState: {
    todos: [
        {
            title: "Get coffee"
        }
    ]
  }
})

Config Options

KeyExplanation
treeA mobx-state-tree model
idA string ID which will be used to get/set the tree in localStorage
initialStateThe initial state for your store
blacklistKeysAny top-level keys for your store which you don't want to persist in localStorage