mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Using router example improvements (#1034)
This commit is contained in:
parent
4534c55968
commit
c106a1d02f
|
@ -2,30 +2,30 @@ import Router from 'next/router'
|
||||||
|
|
||||||
export default () => (
|
export default () => (
|
||||||
<div>
|
<div>
|
||||||
<Link href='/'><a>Home</a></Link>
|
<Link href='/'>Home</Link>
|
||||||
<Link href='/about'><a>About</a></Link>
|
<Link href='/about'>About</Link>
|
||||||
<Link href='/error'><a>Error</a></Link>
|
<Link href='/error'>Error</Link>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
||||||
// typically you want to use `next/link` for this usecase
|
// typically you want to use `next/link` for this usecase
|
||||||
// but this example shows how you can also access the router
|
// but this example shows how you can also access the router
|
||||||
// and use it manually
|
// and use it manually
|
||||||
const Link = ({ children, href }) => (
|
|
||||||
<a
|
|
||||||
href='#'
|
|
||||||
style={styles.a}
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault()
|
|
||||||
Router.push(href)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{ children }
|
|
||||||
</a>
|
|
||||||
)
|
|
||||||
|
|
||||||
const styles = {
|
function onClickHandler (href) {
|
||||||
a: {
|
return (e) => {
|
||||||
marginRight: 10
|
e.preventDefault()
|
||||||
|
Router.push(href)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Link = ({ children, href }) => (
|
||||||
|
<a href='#' onClick={onClickHandler(href)}>
|
||||||
|
{children}
|
||||||
|
<style jsx>{`
|
||||||
|
a {
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
`}</style>
|
||||||
|
</a>
|
||||||
|
)
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
import React from 'react'
|
import {Component} from 'react'
|
||||||
import Header from '../components/Header'
|
import Header from '../components/Header'
|
||||||
import Router from 'next/router'
|
import Router from 'next/router'
|
||||||
|
|
||||||
const ErrorPage = ({ aa }) => (
|
export default class extends Component {
|
||||||
<div>
|
static getInitialProps () {
|
||||||
<Header />
|
console.log(Router.pathname)
|
||||||
<p>This should not be rendered via SSR</p>
|
return {}
|
||||||
</div>
|
}
|
||||||
)
|
|
||||||
|
|
||||||
ErrorPage.getInitialProps = () => {
|
render () {
|
||||||
console.log(Router.pathname)
|
return (
|
||||||
|
<div>
|
||||||
|
<Header />
|
||||||
|
<p>This should not be rendered via SSR</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ErrorPage
|
|
||||||
|
|
Loading…
Reference in a new issue