2018-12-17 16:34:32 +00:00
|
|
|
import App, { Container } from 'next/app'
|
2018-06-19 19:50:37 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { Provider } from 'unstated'
|
2019-01-06 15:58:06 +00:00
|
|
|
import { counterStore } from '../containers/CounterContainer'
|
2018-06-19 19:50:37 +00:00
|
|
|
|
|
|
|
class MyApp extends App {
|
2019-01-06 15:58:06 +00:00
|
|
|
static async getInitialProps () {
|
|
|
|
// do your server state here
|
|
|
|
if (!process.browser) {
|
|
|
|
// reset state for each request
|
|
|
|
counterStore.resetState()
|
|
|
|
// process state, in this case counter start with 999
|
|
|
|
counterStore.initState(999)
|
|
|
|
return { serverState: counterStore.state }
|
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
// pass the state to client store
|
|
|
|
// serverState will be reset when client navigate with Link
|
|
|
|
if (process.browser) {
|
|
|
|
counterStore.initState(props.serverState.count)
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 19:50:37 +00:00
|
|
|
render () {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { Component, pageProps } = this.props
|
2018-06-19 19:50:37 +00:00
|
|
|
return (
|
|
|
|
<Container>
|
2019-01-06 15:58:06 +00:00
|
|
|
<Provider inject={[counterStore]}>
|
2018-06-19 19:50:37 +00:00
|
|
|
<Component {...pageProps} />
|
|
|
|
</Provider>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MyApp
|