-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
4c29708
commit 618e07c
Showing
7 changed files
with
132 additions
and
16 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 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 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 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,51 @@ | ||
import isDeepEqual from 'fast-deep-equal'; | ||
import { useEffect, useRef } from 'react'; | ||
import { To } from './__types'; | ||
import { useRouter } from './__useRouter'; | ||
|
||
/** | ||
* Prefetches components and data of routes matched by a location after a component has mounted. | ||
* | ||
* @param to A location or a route to prefetch. | ||
* @see {@link Prefetch} | ||
* @group Hooks | ||
* @example | ||
* const userRoute = createRoute('/userRoute/:userId'); | ||
* | ||
* usePrefetch(userRoute.getLocation({ userId: 37 })); | ||
*/ | ||
export function usePrefetch(to: To): void { | ||
const toRef = useRef<To>(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
if (isDeepEqual(toRef.current, to)) { | ||
return; | ||
} | ||
toRef.current = to; | ||
router.prefetch(to); | ||
}, [router, to]); | ||
} | ||
|
||
/** | ||
* Props of the {@link Prefetch} component. | ||
* | ||
* @group Components | ||
*/ | ||
export interface PrefetchProps { | ||
/** | ||
* A location or a route to prefetch. | ||
*/ | ||
to: To; | ||
} | ||
|
||
/** | ||
* Prefetches components and data of routes matched by a location after a component has mounted. | ||
* | ||
* @see {@link usePrefetch} | ||
* @group Components | ||
*/ | ||
export function Prefetch(props: PrefetchProps): null { | ||
usePrefetch(props.to); | ||
return null; | ||
} |
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 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 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