Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor using-router example #40774

Merged
merged 3 commits into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/using-router/components/CustomLink.tsx
@@ -0,0 +1,47 @@
import { useRouter } from 'next/router'
import { useEffect, ReactNode, HTMLAttributes } from 'react'

type CustomLinkProps = {
children: ReactNode
href: string
prefetch?: boolean
replace?: boolean
shallow?: boolean
} & HTMLAttributes<HTMLAnchorElement>

// typically you want to use `next/link` for this usecase
// but this example shows how you can also access the router
ijjk marked this conversation as resolved.
Show resolved Hide resolved
// and use it manually
export default function CustomLink({
children,
href,
prefetch = false,
replace = false,
shallow = false,
...props
}: CustomLinkProps) {
const router = useRouter()

useEffect(() => {
if (prefetch) {
router.prefetch(href)
}
}, [router, href, prefetch])

return (
<a
{...props}
href={href}
onClick={(event) => {
event.preventDefault()
if (replace) {
router.replace(href, undefined, { shallow })
} else {
router.push(href, undefined, { shallow })
}
}}
>
{children}
</a>
)
}
33 changes: 0 additions & 33 deletions examples/using-router/components/Header.js

This file was deleted.

14 changes: 14 additions & 0 deletions examples/using-router/components/Header.tsx
@@ -0,0 +1,14 @@
import CustomLink from './CustomLink'

export default function Header() {
return (
<header>
<nav>
<CustomLink href="/">Home</CustomLink>{' '}
<CustomLink href="/about" prefetch>
About
</CustomLink>
</nav>
</header>
)
}
4 changes: 2 additions & 2 deletions examples/using-router/package.json
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
@@ -1,10 +1,10 @@
import Header from '../components/Header'

export default function About() {
export default function AboutPage() {
return (
<div>
<>
<Header />
<p>This is the about page.</p>
</div>
</>
)
}
@@ -1,10 +1,10 @@
import Header from '../components/Header'

export default function Home() {
export default function IndexPage() {
return (
<div>
<>
<Header />
<p>HOME PAGE is here!</p>
</div>
</>
)
}
20 changes: 20 additions & 0 deletions examples/using-router/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}