DEV Community

Cover image for Building a React Hook That Predicts User Navigation Based on Hover Patterns
HexShift
HexShift

Posted on • Edited on

Building a React Hook That Predicts User Navigation Based on Hover Patterns

Imagine if your app could prefetch pages based on what the user is likely to click — before they actually click it. In this guide, we’ll build a React hook that predicts user navigation based on hover behavior, making your app feel faster without wasteful network calls.

Why Predict Navigation?

Real-world advantages:

  • Prefetching assets and data just-in-time
  • Reducing Time to Interactive (TTI) on key page transitions
  • Enhancing perceived performance for mobile users

Step 1: Build the useHoverIntent Hook

Track hover timing to predict serious "intent" (vs accidental hover):

// useHoverIntent.js import { useState, useEffect, useRef } from "react"; export function useHoverIntent(onIntent, delay = 150) { const timer = useRef(null); const onMouseEnter = () => { timer.current = setTimeout(() => { onIntent(); }, delay); }; const onMouseLeave = () => { clearTimeout(timer.current); }; return { onMouseEnter, onMouseLeave, }; } 

Step 2: Create a usePrefetchOnHover Hook

This hook wires hover intent to a custom prefetch action:

// usePrefetchOnHover.js import { useHoverIntent } from "./useHoverIntent"; export function usePrefetchOnHover(url, prefetchFn) { return useHoverIntent(() => { prefetchFn(url); }); } 

Step 3: Create a Prefetching Function

You can customize prefetching depending on your setup (e.g., Next.js, Remix, custom APIs):

// prefetch.js export async function prefetch(url) { try { await fetch(url, { method: "HEAD" }); // Lightweight pre-request } catch (err) { console.error("Prefetch failed:", err); } } 

Step 4: Use It Inside a Link Component

// SmartLink.js import { usePrefetchOnHover } from "./usePrefetchOnHover"; import { prefetch } from "./prefetch"; function SmartLink({ href, children }) { const hoverHandlers = usePrefetchOnHover(href, prefetch); return ( <a href={href} {...hoverHandlers}> {children} </a> ); } export default SmartLink; 

Step 5: Put It Together

// App.js import SmartLink from "./SmartLink"; function App() { return ( <nav> <SmartLink href="/about">About Us</SmartLink> <SmartLink href="/services">Services</SmartLink> <SmartLink href="/contact">Contact</SmartLink> </nav> ); } export default App; 

Pros and Cons

✅ Pros

  • Predicts real user behavior without AI/ML overhead
  • Prefetches only when there's strong "click intent"
  • Extremely lightweight (one tiny hook)

⚠️ Cons

  • Prefetch timing is tricky — too early = wasted bandwidth, too late = no perceived gain
  • May trigger unnecessary loads if users hover and bounce away often
  • Requires careful UX tuning on mobile (where "hover" can behave differently)

🚀 Alternatives

  • Next.js Link prefetch: Built-in link prefetching on hover (but less customizable)
  • Quicklink: Google Chrome Labs library that auto-prefetches visible links

Summary

Smart prefetching based on hover intent is a secret weapon for frontend performance. With just a few lines of custom React hooks, you can predict where users will go next — and make your app feel instant without wasting resources.

For a much more extensive guide on getting the most out of React portals, check out my full 24-page PDF file on Gumroad. It's available for just $10:

Using React Portals Like a Pro.

If you found this useful, you can support me here: buymeacoffee.com/hexshift

Top comments (0)