Lazy loading helps you optimize performance by loading components only when theyβre needed, not upfront. It's perfect for large apps where not everything should be loaded at once.
π§ Why use lazy loading?
β’ Reduces initial bundle size
β’ Improves app speed and performance
β’ Better user experience on slow networks
π¦ React provides React.lazy() and Suspense for lazy loading components.
π§ Example:
import React, { Suspense } from "react";
const LazyAbout = React.lazy(() => import("./About"));
function App() {
return (
<Suspense fallback={<p>Loading...</p>}>
<LazyAbout />
</Suspense>
);
}
π Key points:
β’ Wrap lazy components in
β’ Use a fallback UI like a loader or message
β’ Great for routes, modals, and large UI blocks
Lazy loading helps deliver faster, more efficient React apps without sacrificing features.
Top comments (0)