2018-12-17 16:34:32 +00:00
|
|
|
import App, { Container } from 'next/app'
|
2018-06-30 20:29:24 +00:00
|
|
|
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 {
|
2018-12-17 16:34:32 +00:00
|
|
|
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
|
2018-11-06 09:18:26 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
let appProps = await App.getInitialProps(appContext)
|
2018-11-06 09:18:26 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
return {
|
|
|
|
...appProps,
|
|
|
|
initialMobxState: mobxStore
|
2018-11-06 09:18:26 +00:00
|
|
|
}
|
2018-12-17 16:34:32 +00:00
|
|
|
}
|
2018-06-30 20:29:24 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
const isServer = typeof window === 'undefined'
|
|
|
|
this.mobxStore = isServer
|
|
|
|
? props.initialMobxState
|
|
|
|
: initializeStore(props.initialMobxState)
|
|
|
|
}
|
2018-11-06 09:18:26 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
render() {
|
|
|
|
const { Component, pageProps } = this.props
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Provider store={this.mobxStore}>
|
|
|
|
<Component {...pageProps} />
|
|
|
|
</Provider>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
2018-11-06 09:18:26 +00:00
|
|
|
}
|
|
|
|
export default MyMobxApp
|