2018-06-30 20:29:24 +00:00
|
|
|
import App, {Container} from 'next/app'
|
|
|
|
import React from 'react'
|
2018-11-06 09:18:26 +00:00
|
|
|
import { initializeStore } from '../store'
|
2018-06-30 20:29:24 +00:00
|
|
|
import { Provider } from 'mobx-react'
|
|
|
|
|
2018-11-06 09:18:26 +00:00
|
|
|
class MyMobxApp extends App {
|
|
|
|
static async getInitialProps(appContext) {
|
|
|
|
// Get or Create the store with `undefined` as initialState
|
|
|
|
// This allows you to set a custom default initialState
|
|
|
|
const mobxStore = initializeStore()
|
|
|
|
// Provide the store to getInitialProps of pages
|
|
|
|
appContext.ctx.mobxStore = mobxStore
|
|
|
|
|
|
|
|
let appProps = await App.getInitialProps(appContext)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...appProps,
|
|
|
|
initialMobxState: mobxStore
|
|
|
|
};
|
|
|
|
}
|
2018-06-30 20:29:24 +00:00
|
|
|
|
2018-11-06 09:18:26 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2018-11-20 14:53:28 +00:00
|
|
|
const isServer = typeof window === 'undefined'
|
|
|
|
this.mobxStore = isServer ?
|
|
|
|
props.initialMobxState:
|
|
|
|
initializeStore(props.initialMobxState)
|
2018-11-06 09:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { Component, pageProps } = this.props
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Provider store={this.mobxStore}>
|
|
|
|
<Component {...pageProps} />
|
|
|
|
</Provider>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default MyMobxApp
|