The Interaction-Based Lazy Loading Pattern
What you'll learn: How to defer loading expensive components until a user actually interacts with a trigger element, keeping your initial bundles lean.
The Core Idea
Not all code needs to load immediately. Some components are hidden behind user actions—modals triggered by buttons, panels that expand on click, dropdowns, or dialogs. The interaction-based lazy loading pattern means loading these components only when the user clicks, hovers, or otherwise signals intent. Until that moment, the code stays off the page entirely.
Why It Matters
Every byte in your initial bundle delays interactivity. If you have a massive "Share" modal that 95% of users never open, why make everyone download it? With interaction-based splitting:
- Initial page load is faster (smaller bundle)
- Most users never pay the cost of unused features
- The component loads just in time when needed
The Pattern
const ShareModal = React.lazy(() => import('./ShareModal'));
function ArticlePage() {
const [showModal, setShowModal] = useState(false);
return (
<>
<button onClick={() => setShowModal(true)}>
Share Article
</button>
{showModal && (
<Suspense fallback={<Spinner />}>
<ShareModal onClose={() => setShowModal(false)} />
</Suspense>
)}
</>
);
}
The ShareModal chunk downloads only when showModal becomes true. The user clicks, sees a brief spinner, then the modal appears. The bundle cost is deferred to the moment of interaction.
When to Use This
Perfect for:
- Modals and dialogs
- Expandable panels
- Admin/power-user features
- Heavy visualizations (charts, editors)
Key Takeaway: Interaction-based lazy loading defers component downloads until users actually need them, optimizing initial load performance by making bundle costs opt-in rather than mandatory.