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 # 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 ## Deploy your own
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "16.13.1",
"react-dom": "^16.7.0" "react-dom": "16.13.1"
}, },
"license": "ISC" "license": "ISC"
} }
import React from 'react' import { useRouter } from 'next/router'
import Link from 'next/link' import Link from 'next/link'
import Router, { withRouter } from 'next/router'
import { format } from 'url' import { format } from 'url'
let counter = 1 let counter = 0
class Index extends React.Component { export async function getServerSideProps() {
static getInitialProps({ res }) { counter++
if (res) { return { props: { initialPropsCounter: counter } }
return { initialPropsCounter: 1 } }
}
counter++
return {
initialPropsCounter: counter,
}
}
reload() { export default function Index({ initialPropsCounter }) {
const { pathname, query } = Router const router = useRouter()
Router.push(format({ pathname, query })) const { pathname, query } = router
const reload = () => {
router.push(format({ pathname, query }))
} }
const incrementCounter = () => {
incrementStateCounter() { const currentCounter = query.counter ? parseInt(query.counter) : 0
const { router } = this.props
const currentCounter = router.query.counter
? parseInt(router.query.counter)
: 0
const href = `/?counter=${currentCounter + 1}` const href = `/?counter=${currentCounter + 1}`
Router.push(href, href, { shallow: true })
}
render() {
const { initialPropsCounter, router } = this.props
return ( router.push(href, href, { shallow: true })
<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>
)
} }
}
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