DEV Community

Aman Kureshi
Aman Kureshi

Posted on

Lazy Loading in React โ€” Load Only What You Need, When You Need It ๐Ÿš€

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)