Skip to content
Advanced PatternsChapter 29 / 37Intermediate5 min read

Suspense & lazy()

React suspense lazy loading explained. Split components with lazy() and show a loading UI with a Suspense fallback, with simple examples.

On this page

1. What are Suspense and lazy()?

Both lazy() and Suspense come from the react package. lazy() takes a dynamic import() and returns a component whose code lives in a separate file (a chunk).

Suspense is a boundary. If any component inside it is not "ready" yet, for example lazy code is still loading or use() is waiting on a Promise, React shows the fallback.

2. Why does it matter?

Without code splitting, all of the app's JavaScript goes into a single bundle. Even if the user never opens the settings page, its code is downloaded first.

Without lazy()With lazy() + Suspense
All code in one big bundleEach heavy part in its own chunk
Slow first loadSmall, fast first load
Rarely used pages are downloaded upfrontCode arrives only when needed
You manage loading state yourselfA declarative Suspense fallback

3. Where is it used?

Use caseExample
Route level splittingEach page's component with lazy()
Heavy widgetsA chart library, a rich text editor, a map
Modals and drawersThings that open only on click
Admin panelsSeen by only some users
Data loadingA loading UI with use() or framework data

4. How does it work?

When React renders a lazy component for the first time, it calls import(). Until the Promise resolves, the component "suspends" and React shows the fallback of the nearest Suspense boundary.

The journey of a lazy component
The component renders for the first time
import() is called, the chunk starts downloading
The component suspends
The nearest Suspense fallback is shown
The chunk loads, the real component renders

On later renders the code is already cached, so the fallback does not appear again.

5. Code example

Example 1: Basic lazy loading

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

const SettingsPanel = lazy(() => import("./SettingsPanel.jsx"));

export default function App() {
  const [open, setOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setOpen(true)}>Open settings</button>
      {open && (
        <Suspense fallback={<p>Loading settings...</p>}>
          <SettingsPanel />
        </Suspense>
      )}
    </div>
  );
}
jsx
export default function SettingsPanel() {
  return <section>Settings go here</section>;
}

How does the code work?

LineMeaning
lazy(() => import(...))Creates a lazy component at the top level of the module
export defaultlazy() needs a default export
open && (...)The code is not downloaded at all before the button click
fallbackThis UI is shown until the chunk arrives

Example 2: Nested boundaries

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

const Sidebar = lazy(() => import("./Sidebar.jsx"));
const Feed = lazy(() => import("./Feed.jsx"));

export default function Dashboard() {
  return (
    <Suspense fallback={<p>Loading page...</p>}>
      <Sidebar />
      <Suspense fallback={<p>Loading feed...</p>}>
        <Feed />
      </Suspense>
    </Suspense>
  );
}

The inner boundary covers only Feed. Once Sidebar arrives it shows up, while Feed shows its own fallback. This way you decide how fine-grained the loading UI is.

Interview tip

If you are asked, "Is Suspense only for lazy?", the answer is no. Suspense works with any suspending source, such as lazy(), use(promise), and data fetching in Suspense-enabled frameworks. A fetch inside useEffect does not trigger Suspense.

6. Common mistakes

Mistake: Calling lazy() inside a component

Why? A new component type is created on every render, so state resets and the code loads again and again.

Better: Always call lazy() at the top level of the module, outside the component.

Mistake: Passing a named export straight to lazy()

Why? lazy() needs a Promise that resolves to a module with a default export, otherwise it throws an error.

Better: Export the component as default, or return it in the shape of a default export inside then after the import.

Mistake: Wrapping the whole app in a single Suspense

Why? Even when a tiny part is loading, the whole page turns into the fallback.

Better: Place boundaries where a loading UI makes sense, such as around each section or route.

Mistake: Not handling load errors

Why? If the network fails, the import rejects and the app can crash.

Better: Put an Error Boundary above Suspense so the user gets a retry option.

7. What to learn before and after

Final quick revision

PointDetail
lazy()A separate chunk for a component, via dynamic import()
SuspenseShows the fallback while something suspends
Placementlazy() at the top level, with a default export
NestingThe inner boundary covers only its own children
ErrorsPair it with an Error Boundary