Skip to content

Commit fa87184

Browse files
author
Florian Didron
committed
Adds navigation in the example
1 parent f76e006 commit fa87184

5 files changed

Lines changed: 64 additions & 19 deletions

File tree

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from 'react'
22
import { inject, observer } from 'mobx-react'
33

4-
const Clock = inject('store')(observer(({store}) => {
4+
export default (props) => {
55
return (
6-
<div className={store.light ? 'light' : ''}>
7-
{format(new Date(store.lastUpdate))}
6+
<div className={props.light ? 'light' : ''}>
7+
{format(new Date(props.lastUpdate))}
88
<style jsx>{`
99
div {
1010
padding: 15px;
11-
display: inline-block;
1211
color: #82FA58;
12+
display: inline-block;
1313
font: 50px menlo, monaco, monospace;
1414
background-color: #000;
1515
}
@@ -20,10 +20,8 @@ const Clock = inject('store')(observer(({store}) => {
2020
`}</style>
2121
</div>
2222
)
23-
}))
23+
}
2424

2525
const format = t => `${pad(t.getHours())}:${pad(t.getMinutes())}:${pad(t.getSeconds())}`
2626

2727
const pad = n => n < 10 ? `0${n}` : n
28-
29-
export default Clock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react'
2+
import Link from 'next/link'
3+
import { inject, observer } from 'mobx-react'
4+
import Clock from '../components/Clock'
5+
6+
@inject('store') @observer
7+
class Page extends React.Component {
8+
9+
componentDidMount () {
10+
this.props.store.start()
11+
}
12+
13+
componentWillUnmount () {
14+
this.props.store.stop()
15+
}
16+
17+
render () {
18+
return (
19+
<div>
20+
<h1>{this.props.title}</h1>
21+
<Clock lastUpdate={this.props.store.lastUpdate} light={this.props.store.light} />
22+
<nav>
23+
<Link href={this.props.linkTo}>Navigate</Link>
24+
</nav>
25+
</div>
26+
)
27+
}
28+
}
29+
30+
export default Page

examples/with-mobx/pages/index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { Provider } from 'mobx-react'
33
import { initStore } from '../store'
4-
import Clock from '../components/Clock'
4+
import Page from '../components/Page'
55

66
export default class Counter extends React.Component {
77
static getInitialProps ({ req }) {
@@ -15,18 +15,10 @@ export default class Counter extends React.Component {
1515
this.store = initStore(props.isServer, props.lastUpdate)
1616
}
1717

18-
componentDidMount () {
19-
this.store.start()
20-
}
21-
22-
componentWillUnmount () {
23-
this.store.stop()
24-
}
25-
2618
render () {
2719
return (
2820
<Provider store={this.store}>
29-
<Clock />
21+
<Page title="Index Page" linkTo="/other" />
3022
</Provider>
3123
)
3224
}

examples/with-mobx/pages/other.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
import { Provider } from 'mobx-react'
3+
import { initStore } from '../store'
4+
import Page from '../components/Page'
5+
6+
export default class Counter extends React.Component {
7+
static getInitialProps ({ req }) {
8+
const isServer = !!req
9+
const store = initStore(isServer)
10+
return { lastUpdate: store.lastUpdate, isServer }
11+
}
12+
13+
constructor (props) {
14+
super(props)
15+
this.store = initStore(props.isServer, props.lastUpdate)
16+
}
17+
18+
render () {
19+
return (
20+
<Provider store={this.store}>
21+
<Page title="Other Page" linkTo="/" />
22+
</Provider>
23+
)
24+
}
25+
}

examples/with-mobx/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Store {
66
@observable lastUpdate = 0
77
@observable light = false
88

9-
constructor(isServer, lastUpdate) {
9+
constructor (isServer, lastUpdate) {
1010
this.lastUpdate = lastUpdate
1111
}
1212

@@ -20,7 +20,7 @@ class Store {
2020
stop = () => clearInterval(this.timer)
2121
}
2222

23-
export function initStore(isServer, lastUpdate = Date.now()) {
23+
export function initStore (isServer, lastUpdate = Date.now()) {
2424
if (isServer && typeof window === 'undefined') {
2525
return new Store(isServer, lastUpdate)
2626
} else {

0 commit comments

Comments
 (0)