Overview
The Lazy Loading example demonstrates how to improve application performance by loading components on-demand:- Lazy component loading with
lazy() - Custom loading components
- Dynamic imports
- Performance optimization
Live Demo
View the complete source code on GitHub
What You’ll Learn
Lazy Loading
Load components only when needed
Custom Loading
Create custom loading indicators
Code Splitting
Split code for better performance
Suspense Pattern
Handle async component loading
Complete Code
Setting Up Lazy Components
lazy-demo.js
Custom Loading Component
Main Application with Lazy Loading
Key Concepts
1. Lazy Function
Thelazy() function creates a lazy-loaded component wrapper:
In production, you would use dynamic imports instead of
createDelayedComponent().2. Loading States
Provide a loading component to display while loading:3. Component Factory Pattern
Lazy components return a factory function:4. Cleanup in Loading Components
Always clean up resources when unmounting:Benefits of Lazy Loading
Reduced Initial Bundle Size
Reduced Initial Bundle Size
Only load the code needed for the initial view, reducing the JavaScript bundle size that users download on first load.
Faster Initial Load
Faster Initial Load
By splitting code into smaller chunks, the initial page load is faster, improving perceived performance.
Better Resource Utilization
Better Resource Utilization
Components are only loaded when needed, reducing memory usage and CPU time spent parsing unused JavaScript.
Improved User Experience
Improved User Experience
Users can start interacting with the app sooner, while additional features load in the background.
Production Usage
In a real application with a bundler, you’d use dynamic imports:Running the Example
Next Steps
Performance
Learn more optimization techniques
Pomodoro Timer
See advanced hooks and effects
Lazy API
Detailed lazy() documentation
Lifecycle
Component lifecycle methods