Unverified Commit 69595adc authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

chore: refactor using-router example (#40774)

## Changes

- Updated dependencies
- Migrated to Typescript
- Added additional router use cases

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 48264c26
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
// 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>
)
}
import { useRouter } from 'next/router'
export default function Header() {
return (
<div>
<Link href="/">Home</Link>
<Link href="/about">About</Link>
</div>
)
}
const Link = ({ children, href }) => {
const router = useRouter()
return (
<a
href="#"
onClick={(e) => {
e.preventDefault()
// typically you want to use `next/link` for this usecase
// but this example shows how you can also access the router
// and use it manually
router.push(href)
}}
>
{children}
<style jsx>{`
a {
margin-right: 10px;
}
`}</style>
</a>
)
}
import CustomLink from './CustomLink'
export default function Header() {
return (
<header>
<nav>
<CustomLink href="/">Home</CustomLink>{' '}
<CustomLink href="/about" prefetch>
About
</CustomLink>
</nav>
</header>
)
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2" "react-dom": "^18.2.0"
} }
} }
import Header from '../components/Header' import Header from '../components/Header'
export default function About() { export default function AboutPage() {
return ( return (
<div> <>
<Header /> <Header />
<p>This is the about page.</p> <p>This is the about page.</p>
</div> </>
) )
} }
import Header from '../components/Header' import Header from '../components/Header'
export default function Home() { export default function IndexPage() {
return ( return (
<div> <>
<Header /> <Header />
<p>HOME PAGE is here!</p> <p>HOME PAGE is here!</p>
</div> </>
) )
} }
{
"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"]
}
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