Fallback UI During Code Split Loading
You'll learn how to design effective loading states that appear while React downloads lazy-loaded JavaScript chunks.
Why Fallback UI Matters
When you use React.lazy() to code-split your app, there's a brief moment where the component's JavaScript hasn't arrived yet. Without a fallback UI, users see nothing — or worse, a broken interface. The fallback prop on your Suspense boundary fills this gap with a loading indicator.
The fallback Prop
The fallback prop accepts any valid React element to display during loading:
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
Think of it like a placeholder sign: "Your component is on the way; here's what to look at meanwhile."
Designing Effective Fallbacks
Your fallback should match the shape of what's loading:
// Simple spinner for small components
<Suspense fallback={<Spinner />}>
<UserProfile />
</Suspense>
// Skeleton UI that mimics the layout
<Suspense fallback={<DashboardSkeleton />}>
<Dashboard />
</Suspense>
Skeleton screens (gray boxes matching your layout) feel faster than spinners because users see structure immediately. For small components, a spinner works fine. For large page sections, invest in a skeleton that previews the content's shape.
Multiple Boundaries, Different Fallbacks
Each Suspense boundary can have its own fallback tailored to what it's loading:
<Suspense fallback={<PageSkeleton />}>
<MainContent />
<Suspense fallback={<SidebarSkeleton />}>
<Sidebar />
</Suspense>
</Suspense>
The inner boundary shows <SidebarSkeleton /> while Sidebar loads, independent of the outer boundary.
Key Takeaway: The fallback prop transforms dead air during code splitting into a polished loading experience — choose spinners for small components and skeleton UIs for complex layouts to set user expectations.