foxhound87
5/7/2016 - 9:24 PM

State Management & Hydration with MobX [Ep. 05] /src/state/hydrate.js

State Management & Hydration with MobX [Ep. 05] /src/state/hydrate.js

import jsonStringifySafe from 'json-stringify-safe';
import { toJSON } from 'mobx';
import initStore from './index';

/**
 Dehydrate (on server)
*/
export function dehydrate(store) {
  // convert store to json
  return jsonStringifySafe(toJSON(store, true));
}

/**
  Rehydrate (on client)
*/
export function rehydrate() {
  // inject initial state into stores
  return initStore(window.__STATE);
}