Loading States for Split Components
You'll learn how to show friendly placeholders (spinners, skeletons) while dynamically imported code chunks are being fetched.
The User Experience Gap
When you use dynamic imports to split your code, there's a brief moment between when the user triggers the load (clicking a tab, navigating to a route) and when the JavaScript arrives. Without feedback, your app appears frozen—users see nothing happening and wonder if it broke.
Loading states fill this gap. Think of it like a "Your table is being prepared" message at a restaurant instead of standing in awkward silence.
Implementing Loading Fallbacks
Most modern frameworks provide built-in mechanisms. In React, React.lazy() works with Suspense to declare what users see during the load:
import { lazy, Suspense } from 'react';
const HeavyChart = lazy(() => import('./HeavyChart'));
function Dashboard() {
return (
<Suspense fallback={<div>Loading chart...</div>}>
<HeavyChart />
</Suspense>
);
}
The fallback prop is your loading state—it displays immediately while HeavyChart.js downloads.
Choosing the Right Feedback
Spinners work for quick loads (under 1 second). For longer waits, skeleton screens (gray placeholder boxes matching your content's shape) feel faster because they show structure immediately. They set expectations about what's coming.
const Skeleton = () => (
<div className="skeleton-chart" style={{width: '100%', height: 300, background: '#eee'}} />
);
<Suspense fallback={<Skeleton />}>
<HeavyChart />
</Suspense>
You can also combine this with the prefetching techniques you learned earlier—prefetch on hover so the loading state barely flashes.
Key Takeaway: Always wrap dynamically imported components with loading states to maintain perceived performance and prevent user confusion during code chunk downloads.