π React Code Splitting: Boost Performance by Loading Only What You Need
π React Code Splitting

Modern React apps grow quickly β more routes, more components, more dependencies.
But hereβs the problem:
π Bigger bundle = Slower load time = Bad user experience
Enter Code Splitting β one of the easiest ways to optimise performance in React.
In this blog, youβll learn:
What Code Splitting is
Why it matters
How to use
React.lazy()&SuspenseRoute-based & Component-based code splitting
Best practices
π§ What is Code Splitting?
Code Splitting means breaking your app into smaller bundles that load only when needed.
This prevents loading everything at once during the initial render.
Without Code Splitting
React loads:
β All components
β All routes
β All library code
β Even pages user may never open!
With Code Splitting
React loads only:
β Initial route (Home)
β Other pages/components loaded lazily when needed
Result β Faster load β© Better UX β Better Lighthouse score π
π‘ How React Supports Code Splitting
React provides built-in APIs:
1οΈβ£ React.lazy()
Used to load a component lazily.
2οΈβ£ Suspense
Used to show a fallback (like a loader) while the component is loading.
βΆοΈ Basic Example: Lazy Loading a Component
import React, { Suspense, lazy } from "react";
const About = lazy(() => import("./About"));
function App() {
return (
<div>
<h1>React Code Splitting</h1>
<Suspense fallback={<p>Loading...</p>}>
<About />
</Suspense>
</div>
);
}
export default App;
π Whatβs happening?
Aboutcomponent is not bundled in the main fileIt loads only when rendered
Suspenseshows a loading placeholder
π₯ Route-Based Code Splitting (React Router)
This is the most common use case.
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Suspense, lazy } from "react";
const Home = lazy(() => import("./pages/Home"));
const Profile = lazy(() => import("./pages/Profile"));
const Contact = lazy(() => import("./pages/Contact"));
function App() {
return (
<BrowserRouter>
<Suspense fallback={<h2>Loading Page...</h2>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/profile" element={<Profile />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}
export default App;
π‘ Each route becomes a separate bundle.
When the user visits /profile, only then React loads Profile.js.
β Code Splitting for Modules (Charts, Editors, Maps)
If you have heavy libraries (Chart.js, Leaflet, Rich Editors), lazy loading saves MBs.
Example: Lazy loading a chart component.
const ChartComponent = lazy(() => import("./ChartComponent"));
Use inside Suspense:
<Suspense fallback={<Spinner />}>
<ChartComponent />
</Suspense>
π§© Splitting Large Lists or Components
If a component is rarely used, make it lazy:
const UserDetails = lazy(() => import("./UserDetails"));
Render:
{showDetails && (
<Suspense fallback={<Loader />}>
<UserDetails />
</Suspense>
)}
β Dynamic Import with Conditions (Advanced)
let Component;
if (user.role === "admin") {
Component = lazy(() => import("./AdminPanel"));
} else {
Component = lazy(() => import("./UserDashboard"));
}
Show a fallback:
<Suspense fallback={<p>Loading dashboard...</p>}>
<Component />
</Suspense>
π§ Common Mistakes to Avoid
β Donβt wrap Suspense around too large parts of the app
β Wrap around lazy components only
β Calling lazy inside loops
β Always define lazy at the top
β Forgetting fallback
β Always define a simple loader
β Creating thousands of tiny chunks
β Split only heavy or independent modules
π Best Practices
β Split large routes (/about, /contact)
β Lazy load admin dashboards
β Lazy load rarely visited components
β Lazy load heavy UI libraries
β Test with Lighthouse or Webpack bundle analyzer
β Use Suspense boundaries near lazy imports
π Benefits of Code Splitting
| Benefit | Description |
| π Faster Load | Smaller initial bundle |
| π― Better UX | User sees content faster |
| π Lower Data Usage | Loads only required code |
| π§ Optimized App | Better mobile performance |
| π SEO Friendly | Faster page load improves ranking |
π Final Thoughts
React Code Splitting is a must-use technique for any modern frontend app.
It improves performance, speeds up load time, and offers a smoother experience.


