Skip to content

Instantly share code, notes, and snippets.

@adavia
Created June 7, 2018 17:14
Show Gist options
  • Select an option

  • Save adavia/c65d67f244b0549795f7c51e33aacc93 to your computer and use it in GitHub Desktop.

Select an option

Save adavia/c65d67f244b0549795f7c51e33aacc93 to your computer and use it in GitHub Desktop.
import AuthStore from './auth';
import TopicStore from './topic';
class Store {
constructor() {
this.authStore = new AuthStore(this);
this.topicStore = new TopicStore(this);
}
}
export default function initStore(isServer) {
let store = null;
if (isServer) {
return new Store(isServer);
} else {
if (store === null) {
store = new Store(isServer);
}
return store;
}
}
import { inject, Provider } from 'mobx-react';
import initStore from './';
export default function withMobX() {
return function(Page) {
function PageWrapper (props) {
const isServer = props.isServer;
const store = { 'store': initStore(isServer) }
const _props = Object.keys(props).reduce((_props, key) => {
_props[key] = props[key];
return _props;
}, {});
const page = React.createElement(Page, _props);
return React.createElement(Provider, store, page);
}
PageWrapper.getInitialProps = async function(ctx) {
const isServer = !!ctx.req;
ctx.isServer = isServer;
ctx['store'] = initStore(isServer);
let _props = null;
if (typeof Page.getInitialProps === 'function') {
_props = await Page.getInitialProps(ctx);
}
const props = { isServer }
if (_props) {
Object.keys(_props).forEach((key) => {
props[key] = _props[key]
});
}
return props;
}
return PageWrapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment