Tool for HR, Hiring Managers, and the Leadership Team

Lazy Loading in React Routes

Lazy Loading in React Routes

Lazy loading in React routes means loading a component only when the user navigates to that route, instead of loading all route components during the initial page load.

This improves:

  • Initial application performance

  • Page load speed

  • User experience

  • Bundle size optimization

It is commonly implemented using:

  • React.lazy()

  • Suspense

Why Lazy Loading is Needed

Imagine your application has many pages:

  • Dashboard

  • Reports

  • Settings

  • Admin Panel

  • Analytics

If all these components are bundled together initially:

  • Large JavaScript bundle

  • Slow first load

  • Poor performance

With lazy loading:

  • Only the required page is downloaded

  • Other pages load on demand

Basic Flow

Without lazy loading:

App starts
   ↓
All route components download immediately

With lazy loading:

App starts
   ↓
Only required route downloads
   ↓
Other routes load when visited

Example Without Lazy Loading

import Home from "./Home";
import About from "./About";

<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/about" element={<About />} />
</Routes>

Problem:

  • Both Home and About are loaded initially.

Example With Lazy Loading

import { lazy, Suspense } from "react";

const Home = lazy(() => import("./Home"));
const About = lazy(() => import("./About"));

function App() {
  return (
    <Suspense fallback={<h2>Loading...</h2>}>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </Suspense>
  );
}

How It Works

Step 1 — lazy()

const About = lazy(() => import("./About"));
  • React dynamically imports the component

  • Creates a separate bundle/chunk

Step 2 — Suspense

<Suspense fallback={<h2>Loading...</h2>}>
  • Displays fallback UI while component downloads

  • Prevents blank screen

Real-World Use Cases

Lazy loading is useful for:

  • Admin pages

  • Analytics dashboards

  • Reports pages

  • Large enterprise apps

  • E-commerce product pages

  • Feature modules

Advantages

1. Faster Initial Load

Only essential code loads first.

2. Better Performance

Reduces bundle size.

3. Improves User Experience

Pages appear faster.

4. Efficient Resource Usage

Downloads code only when needed.

Interview Question:

What happens internally during lazy loading?

When user visits a route:

  1. Browser requests corresponding JS chunk

  2. Component downloads asynchronously

  3. Suspense fallback displays temporarily

  4. Component renders after download

Code Splitting

Lazy loading is part of code splitting.

React + bundlers like:

  • Webpack

  • Vite

split application into smaller chunks automatically.

Route-Level Lazy Loading Example

const Dashboard = lazy(() => import("./pages/Dashboard"));
const Settings = lazy(() => import("./pages/Settings"));
const Profile = lazy(() => import("./pages/Profile"));

Each route becomes a separate bundle.

Best Practices

Lazy load:

  • Large pages

  • Feature modules

  • Admin sections

  • Rarely visited routes

Avoid lazy loading:

  • Very small components

  • Frequently used UI components

  • Navbar/Footer

Common Interview Questions

1. Difference between lazy loading and normal loading?

Normal Loading Lazy Loading
Loads everything initially Loads on demand
Larger bundle Smaller bundle
Slower startup Faster startup

2. Why is Suspense required?

React.lazy() loads components asynchronously.

Suspense provides:

  • Loading UI

  • Better user experience

3. Can lazy loading improve SEO?

For SPAs:

  • It improves performance

  • But SEO depends on SSR/SSG too

Frameworks like:

  • Next.js

handle SEO better.

Advanced Example

<Suspense fallback={<Spinner />}>
  <Routes>
    <Route path="/admin" element={<Admin />} />
  </Routes>
</Suspense>

Custom spinner can be shown while loading.

Interview-Friendly Definition

Lazy loading in React routes is a performance optimization technique where route components are loaded only when the user navigates to them. It is implemented using React.lazy() and Suspense, helping reduce initial bundle size and improve application load time.

Quick Interview Summary

  • Loads routes on demand

  • Uses React.lazy()

  • Requires Suspense

  • Improves performance

  • Supports code splitting

  • Best for large applications