Course contentsShow
← React
Lesson 2293 of 2,39650. Performance: Optimization TechniquesFree lesson

Dynamic Imports Without Suspense

Pattern for lazy-loading utilities or non-component code using import() directly.

Dynamic Imports Without Suspense

What you'll learn: How to use dynamic import() for lazy-loading non-component code like utilities or libraries without requiring React's Suspense boundary.

The Core Idea

Not everything you lazy-load needs to be a React component wrapped in React.lazy(). When you want to dynamically import utilities, libraries, or helper functions, you can use JavaScript's native import() directly—it returns a Promise that resolves to the module.

This pattern is perfect for code that runs in response to user actions (like opening a modal that needs a heavy charting library) or for deferring non-critical utilities until they're actually needed.

How It Works

async function handleExportData(data) {
  // Import a heavy library only when the user clicks "Export"
  const { saveAs } = await import('file-saver');
  const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
  saveAs(blob, 'data.json');
}

The import() call happens inline—no Suspense, no special setup. The async nature means you typically use it inside event handlers or effects where you can await the result.

When to Use This Pattern

  • Heavy utilities: Date formatting libraries, validation schemas, chart renderers
  • Event-driven features: Export functions, PDF generators, advanced form validation
  • Conditional logic: Load polyfills only for older browsers
  • Non-render code: Anything that doesn't directly return JSX

Unlike component splitting, there's no loading UI to manage—the operation just takes a bit longer the first time.

Key Takeaway: Dynamic import() lets you lazy-load any JavaScript module on-demand without Suspense, perfect for deferring utilities and libraries until the moment they're actually needed.