Skip to content

Commit c106a1d

Browse files
timneutkensarunoda
authored andcommitted
Using router example improvements (vercel#1034)
1 parent 4534c55 commit c106a1d

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

examples/using-router/components/Header.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ import Router from 'next/router'
22

33
export default () => (
44
<div>
5-
<Link href='/'><a>Home</a></Link>
6-
<Link href='/about'><a>About</a></Link>
7-
<Link href='/error'><a>Error</a></Link>
5+
<Link href='/'>Home</Link>
6+
<Link href='/about'>About</Link>
7+
<Link href='/error'>Error</Link>
88
</div>
99
)
1010

1111
// typically you want to use `next/link` for this usecase
1212
// but this example shows how you can also access the router
1313
// and use it manually
14-
const Link = ({ children, href }) => (
15-
<a
16-
href='#'
17-
style={styles.a}
18-
onClick={(e) => {
19-
e.preventDefault()
20-
Router.push(href)
21-
}}
22-
>
23-
{ children }
24-
</a>
25-
)
2614

27-
const styles = {
28-
a: {
29-
marginRight: 10
15+
function onClickHandler (href) {
16+
return (e) => {
17+
e.preventDefault()
18+
Router.push(href)
3019
}
3120
}
21+
22+
const Link = ({ children, href }) => (
23+
<a href='#' onClick={onClickHandler(href)}>
24+
{children}
25+
<style jsx>{`
26+
a {
27+
margin-right: 10px;
28+
}
29+
`}</style>
30+
</a>
31+
)

examples/using-router/pages/error.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import React from 'react'
1+
import {Component} from 'react'
22
import Header from '../components/Header'
33
import Router from 'next/router'
44

5-
const ErrorPage = ({ aa }) => (
6-
<div>
7-
<Header />
8-
<p>This should not be rendered via SSR</p>
9-
</div>
10-
)
5+
export default class extends Component {
6+
static getInitialProps () {
7+
console.log(Router.pathname)
8+
return {}
9+
}
1110

12-
ErrorPage.getInitialProps = () => {
13-
console.log(Router.pathname)
11+
render () {
12+
return (
13+
<div>
14+
<Header />
15+
<p>This should not be rendered via SSR</p>
16+
</div>
17+
)
18+
}
1419
}
15-
16-
export default ErrorPage

0 commit comments

Comments
 (0)