Course contentsShow
← React
Lesson 1356 of 2,39634. Suspense FundamentalsFree lesson

Preloading Lazy Components on Hover

Trigger dynamic imports early on user interaction to reduce perceived load time.

Preloading Lazy Components on Hover

What you'll learn: How to trigger code-splitting loads before a user clicks, making lazy components feel instant.

The Perceived Performance Problem

When you use React.lazy(), the component code doesn't download until React tries to render it—typically after a user clicks a link. This creates a delay: click → network request → download → render. Users notice this lag.

The solution? Preload the lazy component when the user hovers over the button or link, but before they click. By the time they click (usually 100-500ms later), the code is already loaded or nearly finished.

How It Works

Remember that React.lazy() wraps a dynamic import() call. You can call that same import() function manually to trigger the download early:

const LazyDashboard = React.lazy(() => import('./Dashboard'));

// Store the import promise so we can preload it
const preloadDashboard = () => import('./Dashboard');

function App() {
  return (
    <Link 
      to="/dashboard"
      onMouseEnter={preloadDashboard}  // Trigger download on hover
      onFocus={preloadDashboard}       // Also for keyboard users
    >
      Go to Dashboard
    </Link>
  );
}

When the user hovers, preloadDashboard() starts fetching the JavaScript bundle. When they click and navigate, React.lazy() reuses the already-loaded module—no extra wait time.

The Analogy

Think of it like a restaurant pre-cooking popular dishes during slow moments. When a customer orders, the food is ready immediately instead of starting from scratch.

Key Takeaway: Trigger the same import() statement used in React.lazy() during hover or focus events to preload code chunks, eliminating the perceived delay when users interact with your app.