GithubHelp home page GithubHelp logo

mobx-state-tree-persist's Introduction

Mobx-State-Tree Persist

Easily persist and rehydrate your MobX-State-Tree data stores.

Usage

Configure which nodes from data tree should be persisted and use the persist API. Each node can be individually configured:

Setup

const BearModel = types.model('bear')
  .props({
    number: types.optional(types.number, 0)
  });

const FishModel = types.model('fish')
  .props({
    number: types.optional(types.number, 0)
  });

const RootModel = types.model('root')
  .props({
    fish: FishModel,
    bear: BearModel,
  });

const rootStore = RootModel.create({
  fish: {},
  bear: {},
});

const persistStore = persist([
  [rootStore.bear, {key: "bear", storage: AsyncStorage}],
  [rootStore.fish, {key: "fish", storage: AsyncStorage}],
])

Rehydrated?

The call to persist returns a state tree node in intself that can obvserved in any component. The node contains a isRehydrated computed value that flips to true once each configured store has had a chance to load data.

// Example in React
import {observer} from 'mobx-react-lite';

const App = observer(() => {

  // return 'null' until store rehydrated
  if (!persistStore.isRehydrated) {
    return null;
  }

  return (
    // app content
  );
});

Options

option Description
key required - key which the data is stored within the Storage Engine.
storage required - Storage Engine the node is saved within. Any engine can be used that implements the Storage Interface
delay optional - delay timeout in ms before data should be written out to Storage Engine. Default value: 0
whitelist optional - configure which keys from the snapshot should be persisted. Default: all keys.

Storage Interface

Any Storage Engine may be used that implements the following interface:

export type Storage = {
  getItem: (key: string) => Promise<string | null | undefined>;
  setItem: (key: string, data: string) => Promise<void>;
};

mobx-state-tree-persist's People

Contributors

carloskelly13 avatar github-actions[bot] avatar gksander avatar keithluchtel avatar peterlazar1993 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.