Repository navigation
Expose intent-prefetch behavior for custom navigation controls #15457
aleclarson
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
Would React Router consider exposing a public hook for applying its prefetch behaviors to custom elements?
React Router already exposes
PrefetchPageLinksfor prefetching outside<Link>, but consumers must independently implement the interaction behavior behindprefetch="intent"—including hover, focus, touch, delay, cancellation, and event-handler composition.Motivation
We have a navigation component that deliberately renders a
<button>and callsuseNavigate:A control whose sole purpose is ordinary navigation should generally be an anchor. However, applications can also have controls with intentional button or composite-widget semantics that result in navigation.
Those controls can render
PrefetchPageLinks, but there is no public equivalent of<Link prefetch="intent">for connecting the prefetch lifecycle to the control.Current workaround
A consumer can combine
useHrefandPrefetchPageLinks, but must reproduce React Router’s intent behavior:React Router already implements this behavior internally in
usePrefetchBehavior. Copying it downstream risks behavioral differences as React Router’s prefetch policy evolves.Possible API
One option would be to expose a generalized version of the existing helper:
The current internal helper is constrained to
HTMLAnchorElement; a public version could potentially supportHTMLElementor a suitable generic element type.A higher-level hook that accepts the destination and behavior would also address the use case. The specific API is less important than allowing custom controls to reuse React Router’s prefetch policy instead of reimplementing it.
Questions
usePrefetchBehaviorintentionally internal?prefetch="intent"behavior to a non-Linkelement?All reactions