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

Handling Loading States in Interaction-Based Splits

Show spinners or disable triggers during lazy import to communicate loading without jarring blank states.

Handling Loading States in Interaction-Based Splits

What you'll learn: How to show visual feedback (spinners or disabled states) while a lazily-loaded component is being imported, preventing jarring blank states during user interactions.

The Problem with Silent Loading

When you lazy-load a component on hover or click, there's a brief moment between the user's interaction and the component appearing. Without feedback, the user might think nothing happened—they'll click again, get confused, or assume your app is broken. It's like pressing an elevator button that doesn't light up: you have no idea if it registered your request.

Show Progress, Disable Triggers

The solution is two-fold:

  1. Show a loading indicator (spinner, skeleton, or status text)
  2. Disable the trigger temporarily (button becomes disabled, hover becomes non-interactive)

For example, when a user clicks "Open Advanced Settings," you might:

  • Immediately disable the button and show a loading spinner inside it
  • Let Suspense handle the actual component load in its boundary
  • Once the lazy import resolves, the button re-enables and the content appears
function SettingsButton() {
  const [loading, setLoading] = React.useState(false);
  
  const handleClick = () => {
    setLoading(true);
    // Trigger that causes lazy component to load
    // (Suspense boundary will show its own fallback too)
  };
  
  return (
    <button onClick={handleClick} disabled={loading}>
      {loading ? 'Loading...' : 'Open Settings'}
    </button>
  );
}

This combines local UI state (button feedback) with Suspense boundaries (component-level fallback), creating a smooth, communicative loading experience.

Key Takeaway: Always provide immediate visual feedback when interaction triggers lazy loading—use spinners, disabled states, or status text to bridge the gap between user action and component appearance.