2017-03-06 16:48:35 +00:00
|
|
|
import React from 'react'
|
|
|
|
import Link from 'next/link'
|
2018-08-13 18:09:45 +00:00
|
|
|
import Router, { withRouter } from 'next/router'
|
2017-03-06 16:48:35 +00:00
|
|
|
import { format } from 'url'
|
|
|
|
|
|
|
|
let counter = 1
|
|
|
|
|
2018-08-13 18:09:45 +00:00
|
|
|
class Index extends React.Component {
|
2017-03-06 16:48:35 +00:00
|
|
|
static getInitialProps ({ res }) {
|
|
|
|
if (res) {
|
|
|
|
return { initialPropsCounter: 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
counter++
|
|
|
|
return {
|
|
|
|
initialPropsCounter: counter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reload () {
|
|
|
|
const { pathname, query } = Router
|
|
|
|
Router.push(format({ pathname, query }))
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementStateCounter () {
|
2018-08-13 18:09:45 +00:00
|
|
|
const { router } = this.props
|
|
|
|
const currentCounter = router.query.counter
|
|
|
|
? parseInt(router.query.counter)
|
|
|
|
: 0
|
2017-03-06 16:48:35 +00:00
|
|
|
const href = `/?counter=${currentCounter + 1}`
|
|
|
|
Router.push(href, href, { shallow: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-08-13 18:09:45 +00:00
|
|
|
const { initialPropsCounter, router } = this.props
|
2017-03-06 16:48:35 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h2>This is the Home Page</h2>
|
2018-08-13 18:09:45 +00:00
|
|
|
<Link href='/about'>
|
|
|
|
<a>About</a>
|
|
|
|
</Link>
|
2017-03-06 16:48:35 +00:00
|
|
|
<button onClick={() => this.reload()}>Reload</button>
|
2018-08-13 18:09:45 +00:00
|
|
|
<button onClick={() => this.incrementStateCounter()}>
|
|
|
|
Change State Counter
|
|
|
|
</button>
|
2017-03-06 16:48:35 +00:00
|
|
|
<p>"getInitialProps" ran for "{initialPropsCounter}" times.</p>
|
2018-08-13 18:09:45 +00:00
|
|
|
<p>
|
|
|
|
Counter: "{router.query.counter || 0}
|
|
|
|
".
|
|
|
|
</p>
|
2017-03-06 16:48:35 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 18:09:45 +00:00
|
|
|
|
|
|
|
export default withRouter(Index)
|