React.lazy() enables component-level code splitting, allowing parts of an application to load only when required. This reduces initial bundle size and improves performance.
import()<Suspense>
// lazy() dynamically imports a component
const MyComponent = React.lazy(() => import("./MyComponent"));
All components load together during app startup, increasing load time.
// All components bundled at once
import Header from "./Header";
import Footer from "./Footer";
import Dashboard from "./Dashboard";
Components are loaded only when required.
// Components are loaded on demand
const Dashboard = React.lazy(() => import("./Dashboard"));
// Pages load only when route is visited
const Home = React.lazy(() => import("./pages/Home"));
When a user navigates to a lazily loaded component, React fetches the required JavaScript chunk and displays the fallback UI until loading completes.
Initial Load ➜ Core Bundle ➜ User Action ➜ Lazy Component Loaded
Click the button below to simulate loading a "Heavy Dashboard" component over a slow network. Watch how the fallback (Suspense) appears before the content.
(Simulates network latency)
<Suspense>React.lazy()