Skip to content

Lazy Component

In one line

React.lazy + Suspense enables code-splitting — components load on demand, reducing initial bundle size.

In simple words

React.lazy lets you load components on demand via dynamic import(). The component is split into a separate chunk, loaded only when first rendered.

Wrap with Suspense to show a fallback while loading. This reduces the initial bundle size — users only download code for routes/features they actually use.

Common pattern: lazy-load route components so each page is a separate chunk. The user navigates to a page, the chunk loads (with a loading spinner), then the page renders.

Example

jsx
import { lazy, Suspense } from "react";

// Lazy load — creates a separate chunk
const AdminDashboard = lazy(() => import("./AdminDashboard"));
const Settings = lazy(() => import("./Settings"));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Router>
        <Route path="/admin" element={<AdminDashboard />} />
        <Route path="/settings" element={<Settings />} />
      </Router>
    </Suspense>
  );
}

React.lazy splits AdminDashboard and Settings into separate chunks. They load on demand when the user navigates to those routes.

Common confusions

  • Confused with: dynamic import

    The difference: React.lazy wraps a dynamic import() specifically for React components — it returns a component that can be rendered. A plain dynamic import() returns a promise you must handle yourself. React.lazy + Suspense handles the loading state declaratively.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary