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

Dynamic import() Syntax

Learning the native JavaScript dynamic import that React.lazy builds upon for asynchronous module loading.

Dynamic import() Syntax

What you'll learn: How JavaScript's native import() function enables on-demand module loading that React.lazy builds upon.

The Core Mechanism

Before React.lazy existed, JavaScript introduced dynamic imports — a way to load modules asynchronously at runtime instead of at the top of your file. Unlike static import Component from './Component' statements that bundle everything upfront, import() is a function that returns a Promise resolving to the module.

Think of it like ordering food: static imports are meal-prepping everything Sunday night, while dynamic imports are ordering delivery only when you're hungry.

The Syntax

// Static import (traditional, synchronous)
import Button from './Button';

// Dynamic import (function call, asynchronous)
import('./Button').then(module => {
  const Button = module.default;
  // Use Button here
});

The import() function accepts a module path and returns a Promise that resolves to the module object. For default exports, you access module.default. For named exports, you'd use module.ComponentName.

Why This Matters for React

Dynamic imports are pure JavaScript — they work anywhere, not just in React. However, React.lazy wraps this pattern to make it declarative and compatible with Suspense boundaries. Under the hood, React.lazy(() => import('./Component')) is literally calling this dynamic import function.

This native browser feature is what enables webpack, Vite, and other bundlers to automatically split your code into separate chunks that load on-demand.

Key Takeaway: Dynamic import() is JavaScript's built-in Promise-based syntax for loading modules asynchronously — it's the foundation that React.lazy uses to implement code splitting without requiring custom loader logic.