-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
50 additions
and
25 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
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,49 @@ | ||
--- | ||
title: useNextLink (Next.js App Router) | ||
--- | ||
|
||
# useNextLink (Next.js App Router) | ||
|
||
import ExportMetaInfo from '../components/export-meta-info'; | ||
|
||
<ExportMetaInfo /> | ||
|
||
Build your own `next/link` component with all Next.js features you love (client-side navigation between routes, prefetching), plus React's `isPending` transition state to build "navigating..." animation. | ||
|
||
import { Callout } from 'nextra/components' | ||
|
||
<Callout type="warning" emoji="⚠️"> | ||
The `useNextLink` hook is your last resort. You should always prefer `next/link` when possible. This hook can only be used with Next.js App Router, and should only be used when using the `isPending` transition state to build "navigating..." animation. | ||
</Callout> | ||
|
||
```tsx | ||
'use client'; | ||
|
||
import { useRef } from 'react'; | ||
import { useNextLink } from 'foxact/use-next-link'; | ||
|
||
export default function Page() { | ||
const ref = useRef<HTMLAnchorElement>(null); | ||
|
||
const [isPending, linkProps] = useNextLinkProps( | ||
// `href` | ||
'/about', | ||
// optional, your usual next/link prop like "onClick", "prefetch", "replace" and "scroll" should go here. | ||
{ | ||
// If you want to attach your ref to the link, you should put your "ref" here instead of doing <a ref={ref} {...linkProps} /> | ||
ref | ||
} | ||
); | ||
|
||
return ( | ||
<div> | ||
{isPending && <div>Navigating...</div>} | ||
{/** You can only pass non-"next/link" prop directly to the <a /> */} | ||
{/** All "next/link" prop and "ref" should be passed to useNextLink() */} | ||
<a className="link" {...linkProps}> | ||
About | ||
</a> | ||
</div> | ||
); | ||
} | ||
``` |
File renamed without changes.