Commit a25359bf authored by Junyoung Choi (Sai)'s avatar Junyoung Choi (Sai) Committed by Tim Neutkens

Improve with-mobx example (#4705)

in flavor of https://github.com/zeit/next.js/pull/4377
parent de6bf4e7
import React from 'react'
import {initializeStore} from '../store'
const isServer = typeof window === 'undefined'
const __NEXT_MOBX_STORE__ = '__NEXT_MOBX_STORE__'
function getOrCreateStore(initialState) {
// Always make a new store if server, otherwise state is shared between requests
if (isServer) {
return initializeStore(initialState)
}
// Create store if unavailable on the client and set it on the window object
if (!window[__NEXT_MOBX_STORE__]) {
window[__NEXT_MOBX_STORE__] = initializeStore(initialState)
}
return window[__NEXT_MOBX_STORE__]
}
export default (App) => {
return class AppWithMobx extends React.Component {
static async getInitialProps (appContext) {
// Get or Create the store with `undefined` as initialState
// This allows you to set a custom default initialState
const mobxStore = getOrCreateStore()
// Provide the store to getInitialProps of pages
appContext.ctx.mobxStore = mobxStore
let appProps = {}
if (typeof App.getInitialProps === 'function') {
appProps = await App.getInitialProps.call(App, appContext)
}
return {
...appProps,
initialMobxState: mobxStore
}
}
constructor(props) {
super(props)
this.mobxStore = getOrCreateStore(props.initialMobxState)
}
render() {
return <App {...this.props} mobxStore={this.mobxStore} />
}
}
}
import App, {Container} from 'next/app'
import React from 'react'
import withMobxStore from '../lib/with-mobx-store'
import { Provider } from 'mobx-react'
class MyApp extends App {
render () {
const {Component, pageProps, mobxStore} = this.props
return (
<Container>
<Provider store={mobxStore}>
<Component {...pageProps} />
</Provider>
</Container>
)
}
}
export default withMobxStore(MyApp)
import React from 'react' import React from 'react'
import { Provider } from 'mobx-react'
import { initStore } from '../store'
import Page from '../components/Page' import Page from '../components/Page'
export default class Counter extends React.Component { export default class Counter extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(isServer)
return { lastUpdate: store.lastUpdate, isServer }
}
constructor (props) {
super(props)
this.store = initStore(props.isServer, props.lastUpdate)
}
render () { render () {
return ( return (
<Provider store={this.store}> <Page title='Index Page' linkTo='/other' />
<Page title='Index Page' linkTo='/other' />
</Provider>
) )
} }
} }
import React from 'react' import React from 'react'
import { Provider } from 'mobx-react'
import { initStore } from '../store'
import Page from '../components/Page' import Page from '../components/Page'
export default class Counter extends React.Component { export default class Counter extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(isServer)
return { lastUpdate: store.lastUpdate, isServer }
}
constructor (props) {
super(props)
this.store = initStore(props.isServer, props.lastUpdate)
}
render () { render () {
return ( return (
<Provider store={this.store}> <Page title='Other Page' linkTo='/' />
<Page title='Other Page' linkTo='/' />
</Provider>
) )
} }
} }
...@@ -20,7 +20,7 @@ class Store { ...@@ -20,7 +20,7 @@ class Store {
stop = () => clearInterval(this.timer) stop = () => clearInterval(this.timer)
} }
export function initStore (isServer, lastUpdate = Date.now()) { export function initializeStore (isServer, lastUpdate = Date.now()) {
if (isServer) { if (isServer) {
return new Store(isServer, lastUpdate) return new Store(isServer, lastUpdate)
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment