Using React.lazy and Suspense
0 757
🧩 Using React.lazy and Suspense in React
When building large React applications, it's essential to keep the initial load fast. One powerful tool React gives us is code splitting, which loads only what's needed. React’sReact.lazy and Suspense make this seamless. Let’s explore how you can use them to improve app performance with zero third-party libraries. ⚡
📦 What Is Code Splitting?
Code splitting means breaking your JavaScript bundle into smaller chunks and loading them only when necessary. This reduces your app’s initial bundle size and improves load time. React achieves this throughReact.lazy for dynamic imports and Suspense to show fallback content while loading.
âš™ï¸ Syntax of React.lazy
React.lazy lets you load a component only when it’s rendered for the first time:
import React, { Suspense } from 'react';
// Lazy-loaded component
const LazyAbout = React.lazy(() => import('./About'));
function App() {
return (
<div>
<h1>My App</h1>
<Suspense fallback={<p>Loading...</p>}>
<LazyAbout />
</Suspense>
</div>
);
}
- 📦 The
Aboutcomponent will be split into a separate JS chunk - 🔄 The fallback is shown until the component loads
🎬 Where Should You Use It?
UseReact.lazy for components that:
- 📠Live on different pages (e.g., route-based code splitting)
- 📉 Are large or rarely used (charts, editors, dashboards)
- 💡 Don't affect the initial page view
🧠Lazy with React Router
React.lazy works great with routing libraries like React Router:
import { BrowserRouter, Route, Routes } from 'react-router-dom';
const Home = React.lazy(() => import('./Home'));
const About = React.lazy(() => import('./About'));
function App() {
return (
<BrowserRouter>
<Suspense fallback={<div>Loading page...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}
â³ What Is Suspense?
Suspense is a React component that wraps lazy-loaded content and shows a fallback UI (like a loader) while waiting:
<Suspense fallback={<Spinner />}>
<LazyComponent />
</Suspense>
You can use it anywhere in your component tree — even for nested routes, modals, or charts.
🚫 Common Pitfalls
- â›” Lazy loading only works for default exports
- âš ï¸ Make sure the fallback UI is appropriate (don’t use loaders everywhere)
- 📉 Don’t overuse lazy — it can harm UX with too much loading
🚀 Performance Benefits
- 📦 Smaller initial JS bundle size
- âš¡ Faster page loads and time to interactive
- 💻 Only load what the user actually sees
🔀 Advanced Usage: Multiple Suspense Boundaries
You can wrap individual components in separate Suspense boundaries for granular loading control:
<Suspense fallback={<SidebarSkeleton />}>
<Sidebar />
</Suspense>
<Suspense fallback={<ChartSkeleton />}>
<HeavyChart />
</Suspense>
This keeps the rest of the UI responsive while only part of it is loading.
📋 Summary
- ðŸ§
React.lazylets you dynamically import components - â³
Suspenseprovides a loading fallback UI - ðŸ—‚ï¸ Combine both for route-based or component-level code splitting
- âš ï¸ Don't overuse — lazy load only where it improves performance
🚀 Final Thoughts
WithReact.lazy and Suspense, you don’t need fancy tools to speed up your React apps. Just sprinkle lazy loading where needed and keep your bundle lean. Combine this with route-level splitting and server components in React 19, and you’ll have both blazing speed and clean architecture. 🔥⚛ï¸If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:




Comments
Waiting for your comments