1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-mobx-state-tree/pages/_app.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

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 {
static async getInitialProps ({ Component, router, ctx }) {
//
// Use getInitialProps as a step in the lifecycle when
// we can initialize our store
//
const isServer = (typeof window === 'undefined')
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>
)
}
}