Course contentsShow
← React
Lesson 1388 of 2,39635. Code SplittingFree lesson

The fallback Prop: Loading UI

Providing a loading spinner or skeleton UI via the fallback prop while the lazy component loads.

The fallback Prop: Loading UI

What you'll learn: How to provide a smooth loading experience with fallback UI while your lazy-loaded component downloads.

Why fallback Matters

When you use React.lazy() and <Suspense>, there's a brief moment when React is downloading the code-split chunk from the server. Without a fallback, users see nothing—or worse, the app freezes. The fallback prop lets you show a friendly loading indicator during this wait.

The Core Idea

Think of fallback as the "please wait" screen at a doctor's office. When you check in, you don't just stand in silence staring at a door—you're given a waiting room with chairs and magazines. Similarly, fallback gives your users something to look at while React fetches the real component.

<Suspense fallback={<div>Loading...</div>}>
  <LazyComponent />
</Suspense>

Here, <div>Loading...</div> appears immediately when LazyComponent starts loading. Once the component code arrives and renders, React swaps it in.

Common Fallback Patterns

Spinners for quick actions:

fallback={<div className="spinner" />}

Skeleton screens for better UX—they mimic the shape of the incoming content:

fallback={<div className="skeleton-card" />}

Text messages for clarity:

fallback={<p>Fetching dashboard...</p>}

The fallback can be any valid React element. Choose based on expected load time and user context.

Key Takeaway

The fallback prop transforms invisible loading delays into deliberate, user-friendly loading states—always provide one to avoid jarring blank screens during code splitting.