Course contentsShow
← Web Development
Lesson 1413 of 2,98225. Build Tools and BundlersFree lesson

Component-Level Code Splitting

Splitting large components that aren't immediately needed on page load.

Component-Level Code Splitting

What you'll learn: How to defer loading large React/Vue components until they're actually needed, keeping your initial page load fast and lean.

The Core Idea

While route-based code splitting breaks your app into page-level chunks, component-level code splitting goes deeper—it splits individual components that might be heavy but aren't critical for the initial render. Think of a modal dialog, a rich text editor, or a complex data visualization chart. Users might not interact with these immediately (or at all), so why load them upfront?

This is especially powerful for components that are conditionally rendered (like modals triggered by user action) or below the fold.

How It Works

You use the same dynamic import() syntax you learned earlier, but at the component level instead of the route level:

// Instead of importing upfront:
// import HeavyChart from './HeavyChart';

// Import only when needed:
const HeavyChart = lazy(() => import('./HeavyChart'));

function Dashboard() {
  const [showChart, setShowChart] = useState(false);
  
  return (
    <div>
      <button onClick={() => setShowChart(true)}>
        Show Analytics
      </button>
      
      {showChart && (
        <Suspense fallback={<div>Loading chart...</div>}>
          <HeavyChart />
        </Suspense>
      )}
    </div>
  );
}

The HeavyChart component and all its dependencies are only downloaded when showChart becomes true. Until then? Zero bytes transferred.

When to Split Components

Target components that are:

  • Heavy (lots of code or dependencies)
  • Conditionally rendered
  • Below the fold
  • Used in modals/drawers/tabs

Key Takeaway: Component-level code splitting lets you defer loading large UI pieces until users actually need them, dramatically reducing your initial JavaScript bundle while keeping the full feature set available on demand.