-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use prefetch attribute instead of router.prefetch()
- Loading branch information
1 parent
f319e56
commit 51cef9f
Showing
1 changed file
with
8 additions
and
12 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 |
---|---|---|
@@ -1,27 +1,23 @@ | ||
"use client"; | ||
|
||
import NextLink from "next/link"; | ||
import { useRouter } from "next/navigation"; | ||
import type { ComponentProps } from "react"; | ||
import { useState, type ComponentProps } from "react"; | ||
|
||
type Props = ComponentProps<typeof NextLink>; | ||
|
||
export const Link = (props: Props) => { | ||
const { prefetch, href, onMouseEnter, ...rest } = props; | ||
const router = useRouter(); | ||
const { prefetch, onMouseEnter, ...rest } = props; | ||
|
||
const [_prefetch, setPrefetch] = useState( | ||
prefetch === undefined ? false : prefetch, | ||
); | ||
|
||
const _prefetch = prefetch === undefined ? false : prefetch; | ||
const _onMouseEnter = | ||
prefetch === undefined && onMouseEnter === undefined | ||
? () => router.prefetch(typeof href === "string" ? href : href.href) | ||
? () => setPrefetch(true) | ||
: onMouseEnter; | ||
|
||
return ( | ||
<NextLink | ||
href={href} | ||
prefetch={_prefetch} | ||
onMouseEnter={_onMouseEnter} | ||
{...rest} | ||
/> | ||
<NextLink prefetch={_prefetch} onMouseEnter={_onMouseEnter} {...rest} /> | ||
); | ||
}; |