Unverified Commit d093dd57 authored by David Janda's avatar David Janda Committed by GitHub

Updated with-shallow-routing to use getServerSideProps (#11585)

* Replaced getInitialProps with getServerSideProps

* Updated page and readme

* Removed unrequired props

* Mention getStaticProps too
Co-authored-by: 's avatarLuis Alvarez <luis@zeit.co>
parent 2bfbfea6
# Shallow Routing Example
With shallow routing, we could change the URL without actually running the `getInitialProps` every time you change the URL.
With [Shallow Routing](https://nextjs.org/docs/routing/shallow-routing), we can change the URL without running data fetching methods (like `getStaticProps` and `getServerSideProps`) again.
We do this passing the `shallow: true` option to `Router.push` or `Router.replace`.
We do this by passing the `shallow: true` option to `Router.push` or `Router.replace`.
## Deploy your own
......
......@@ -8,8 +8,8 @@
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
"react": "16.13.1",
"react-dom": "16.13.1"
},
"license": "ISC"
}
import React from 'react'
import { useRouter } from 'next/router'
import Link from 'next/link'
import Router, { withRouter } from 'next/router'
import { format } from 'url'
let counter = 1
let counter = 0
class Index extends React.Component {
static getInitialProps({ res }) {
if (res) {
return { initialPropsCounter: 1 }
}
counter++
return {
initialPropsCounter: counter,
}
}
export async function getServerSideProps() {
counter++
return { props: { initialPropsCounter: counter } }
}
reload() {
const { pathname, query } = Router
Router.push(format({ pathname, query }))
export default function Index({ initialPropsCounter }) {
const router = useRouter()
const { pathname, query } = router
const reload = () => {
router.push(format({ pathname, query }))
}
incrementStateCounter() {
const { router } = this.props
const currentCounter = router.query.counter
? parseInt(router.query.counter)
: 0
const incrementCounter = () => {
const currentCounter = query.counter ? parseInt(query.counter) : 0
const href = `/?counter=${currentCounter + 1}`
Router.push(href, href, { shallow: true })
}
render() {
const { initialPropsCounter, router } = this.props
return (
<div>
<h2>This is the Home Page</h2>
<Link href="/about">
<a>About</a>
</Link>
<button onClick={() => this.reload()}>Reload</button>
<button onClick={() => this.incrementStateCounter()}>
Change State Counter
</button>
<p>"getInitialProps" ran for "{initialPropsCounter}" times.</p>
<p>
Counter: "{router.query.counter || 0}
".
</p>
</div>
)
router.push(href, href, { shallow: true })
}
}
export default withRouter(Index)
return (
<div>
<h2>This is the Home Page</h2>
<Link href="/about">
<a>About</a>
</Link>
<button onClick={reload}>Reload</button>
<button onClick={incrementCounter}>Change State Counter</button>
<p>"getServerSideProps" ran for "{initialPropsCounter}" times.</p>
<p>Counter: "{query.counter || 0}".</p>
</div>
)
}
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