All Projects → agilgur5 → mst-persist

agilgur5 / mst-persist

Licence: other
Persist and hydrate MobX-state-tree stores (in < 100 LoC)

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to mst-persist

Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+8322.67%)
Mutual labels:  mobx, mst, state-tree, mobx-state-tree
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (-74.67%)
Mutual labels:  mobx, mst, mobx-state-tree
client-persist
Offline storage for your web client. Supports IndexedDB, WebSQL, localStorage and sessionStorage with an easy to crawl with API.
Stars: ✭ 14 (-81.33%)
Mutual labels:  localstorage, sessionstorage, localforage
vue-vuex-persist
vuex持久化插件
Stars: ✭ 16 (-78.67%)
Mutual labels:  persistence, state, persist
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-57.33%)
Mutual labels:  mobx, state
react-mobx-router
Create React App with React Router 4 and MobX + Internationalization
Stars: ✭ 90 (+20%)
Mutual labels:  mobx, mobx-state-tree
Mobx Persist
persist mobx stores
Stars: ✭ 525 (+600%)
Mutual labels:  mobx, persistence
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (+118.67%)
Mutual labels:  mobx, state
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (+141.33%)
Mutual labels:  persistence, state
Mobx React Form
Reactive MobX Form State Management
Stars: ✭ 1,031 (+1274.67%)
Mutual labels:  mobx, state
Dsladapter
🔥 Kotlin时代的Adapter, Dsl 的形式使用 RecyclerView.Adapter, 支持折叠展开, 树结构,悬停,情感图状态切换, 加载更多, 多类型Item,侧滑菜单等
Stars: ✭ 231 (+208%)
Mutual labels:  tree, state
umi-plugin-mobx
😍 use mobx-state-tree gracefully in umijs.
Stars: ✭ 33 (-56%)
Mutual labels:  mobx, mobx-state-tree
Fancytree
JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading
Stars: ✭ 2,398 (+3097.33%)
Mutual labels:  tree, persistence
mobx-state-tree-router
State-based router for React and MobX State Tree
Stars: ✭ 18 (-76%)
Mutual labels:  mobx, mobx-state-tree
public salaries
Public sector employee salaries
Stars: ✭ 16 (-78.67%)
Mutual labels:  local, state
deadlines
A simple, offline deadline tracker made with Vue.js and localForage.
Stars: ✭ 22 (-70.67%)
Mutual labels:  local, localforage
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (+42.67%)
Mutual labels:  mobx, state
extjs-reactjs-examples
Code examples for ExtJS to React transition
Stars: ✭ 48 (-36%)
Mutual labels:  tree, mobx
Fcuuid
iOS UUID / Universally Unique Identifiers library as alternative to UDID and identifierForVendor. 📱
Stars: ✭ 1,387 (+1749.33%)
Mutual labels:  persistence, session
Vuex Localstorage
Persist Vuex state with expires by localStorage or some else storage.
Stars: ✭ 129 (+72%)
Mutual labels:  persistence, localstorage

mst-persist

package-json releases commits
dt dy dm dw
typings build status code coverage
NPM

Persist and hydrate MobX-state-tree stores.

Installation

npm i -S mst-persist

Usage

import { types } from 'mobx-state-tree'
import localForage from 'localForage'
import { persist } from 'mst-persist'

const SomeStore = types.model('Store', {
  name: 'John Doe',
  age: 32
})

const someStore = SomeStore.create()

persist('some', someStore, {
  storage: localForage,  // or AsyncStorage in react-native.
                         // default: localStorage
  jsonify: false  // if you use AsyncStorage, this shoud be true
                  // default: true
  whitelist: ['name']  // only these keys will be persisted
}).then(() => console.log('someStore has been hydrated'))

API

persist(key, store, options)

  • arguments

    • key string The key of your storage engine that you want to persist to.
    • store MST store The store to be persisted.
    • options object Additional configuration options.
      • storage localForage / AsyncStorage / localStorage Any Storage Engine that has a Promise-style API similar to localForage. The default is localStorage, which has a built-in adaptor to make it support Promises. For React Native, one may configure AsyncStorage instead.
        Any of redux-persist's Storage Engines should also be compatible with mst-persist.
      • jsonify bool Enables serialization as JSON (default: true).
      • whitelist Array<string> Only these keys will be persisted (defaults to all keys).
      • blacklist Array<string> These keys will not be persisted (defaults to all keys).
  • returns a void Promise

Node and Server-Side Rendering (SSR) Usage

Node environments are supported so long as you configure a Storage Engine that supports Node, such as redux-persist-node-storage, redux-persist-cookie-storage, etc. This allows you to hydrate your store server-side.

For SSR though, you may not want to hydrate your store server-side, so in that case you can call persist conditionally:

if (typeof window !== 'undefined') { // window is undefined in Node
  persist(...)
}

With this conditional check, your store will only be hydrated client-side.

Examples

None yet, but can take a look at agilgur5/react-native-manga-reader-app which uses it in production. Can view the commit that implements it here.

How it works

Basically just a small wrapper around MST's onSnapshot and applySnapshot. The source code is currently shorter than this README, so take a look under the hood! :)

Credits

Inspiration for parts of the code and API came from redux-persist, mobx-persist, and this MST persist PoC gist

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].