2018-10-14 08:04:58 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { Provider } from 'mobx-react'
|
|
|
|
import { getSnapshot } from 'mobx-state-tree'
|
|
|
|
import App, { Container } from 'next/app'
|
|
|
|
import { initializeStore } from '../stores/store'
|
|
|
|
|
|
|
|
export default class MyApp extends App {
|
2019-01-02 20:39:38 +00:00
|
|
|
static async getInitialProps ({ Component, ctx }) {
|
2018-10-14 08:04:58 +00:00
|
|
|
//
|
|
|
|
// Use getInitialProps as a step in the lifecycle when
|
|
|
|
// we can initialize our store
|
|
|
|
//
|
2018-12-17 16:34:32 +00:00
|
|
|
const isServer = typeof window === 'undefined'
|
2018-10-14 08:04:58 +00:00
|
|
|
const store = initializeStore(isServer)
|
|
|
|
//
|
|
|
|
// Check whether the page being rendered by the App has a
|
|
|
|
// static getInitialProps method and if so call it
|
|
|
|
//
|
|
|
|
let pageProps = {}
|
|
|
|
if (Component.getInitialProps) {
|
|
|
|
pageProps = await Component.getInitialProps(ctx)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
initialState: getSnapshot(store),
|
|
|
|
isServer,
|
|
|
|
pageProps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.store = initializeStore(props.isServer, props.initialState)
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { Component, pageProps } = this.props
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Provider store={this.store}>
|
|
|
|
<Component {...pageProps} />
|
|
|
|
</Provider>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|