Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
Showing
7 changed files
with
89 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
// 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> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
examples/using-router/pages/about.js → examples/using-router/pages/about.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
</> | ||
) | ||
} |
6 changes: 3 additions & 3 deletions
6
examples/using-router/pages/index.js → examples/using-router/pages/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |