import { Component } from 'react' import Link from 'next/link' import Router, { withRouter } from 'next/router' let getInitialPropsRunCount = 1 const linkStyle = { marginRight: 10, } export default withRouter( class extends Component { static getInitialProps({ res }) { if (res) return { getInitialPropsRunCount: 1 } getInitialPropsRunCount++ return { getInitialPropsRunCount } } getCurrentCounter() { const { router } = this.props return router.query.counter ? parseInt(router.query.counter) : 0 } increase(scroll) { const counter = this.getCurrentCounter() const href = `/nav/shallow-routing?counter=${counter + 1}` Router.push(href, href, { shallow: true, scroll }) } increaseNonShallow() { const counter = this.getCurrentCounter() const href = `/nav/shallow-routing?counter=${counter + 1}` Router.push(href, href, {}) } gotoNavShallow() { const href = `/nav` Router.push(href, href, { shallow: true }) } render() { return (
Home
Counter: {this.getCurrentCounter()}
getInitialProps run count: {this.props.getInitialPropsRunCount}
) } } )