2017-09-23 14:30:22 +00:00
|
|
|
import React from 'react'
|
2018-12-17 16:34:32 +00:00
|
|
|
import { Controller, UniversalController } from 'cerebral'
|
2017-09-23 14:30:22 +00:00
|
|
|
import Devtools from 'cerebral/devtools'
|
|
|
|
import { Container } from '@cerebral/react'
|
|
|
|
import Page from '../components/Page'
|
|
|
|
import clock from '../modules/clock'
|
|
|
|
|
|
|
|
export default class Counter extends React.Component {
|
2018-12-17 16:34:32 +00:00
|
|
|
static getInitialProps ({ req }) {
|
2017-09-23 14:30:22 +00:00
|
|
|
const isServer = Boolean(req)
|
|
|
|
|
|
|
|
// On the server we prepare the state of the application. Since
|
|
|
|
// this is a synchronous state change we just use "setState", but
|
|
|
|
// you could do other async stuff here or even use "runSequence" to
|
|
|
|
// grab and set the initial state of the application using
|
|
|
|
if (isServer) {
|
2018-12-17 16:34:32 +00:00
|
|
|
const controller = UniversalController({ modules: { clock } })
|
2017-09-23 14:30:22 +00:00
|
|
|
controller.setState('clock.lastUpdate', Date.now())
|
|
|
|
|
|
|
|
return { stateChanges: controller.getChanges() }
|
|
|
|
}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
// The controller will be instantiated for every page change and we only
|
|
|
|
// add the devtools if we indeed are running in the browser
|
|
|
|
this.controller = Controller({
|
2018-12-17 16:34:32 +00:00
|
|
|
devtools:
|
|
|
|
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
|
|
|
|
? null
|
|
|
|
: Devtools({ host: 'localhost:8787' }),
|
|
|
|
modules: { clock },
|
2017-09-23 14:30:22 +00:00
|
|
|
stateChanges: props.stateChanges
|
|
|
|
})
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<Container controller={this.controller}>
|
|
|
|
<Page title='Other Page' linkTo='/' />
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|