Featured
- Get link
- X
- Other Apps
⚡ Day 28 — Performance Optimization in React
⚡ Day 28 — Performance Optimization in React
As React apps grow, performance can degrade due to larger bundle sizes and unnecessary re-renders. React provides built-in tools like code splitting, lazy loading, and Suspense to optimize your app for faster load times and smoother performance.
🔹 1. What is Code Splitting?
Code splitting allows you to split your JavaScript bundle into smaller chunks that can be loaded on demand. Instead of loading the entire app at once, only the necessary code for the current route or component is fetched.
import React, { lazy, Suspense } from "react";
const About = lazy(() => import("./About"));
function App() {
return (
<div>
<h1>Welcome to My App</h1>
<Suspense fallback={<p>Loading...</p>}>
<About />
</Suspense>
</div>
);
}
export default App;
🔹 2. Lazy Loading with `React.lazy()`
The React.lazy() function lets you dynamically import components.
This is especially helpful in route-based applications where you don’t want to load all pages at once.
🔹 3. Using `Suspense` for Fallbacks
The Suspense component shows a fallback UI (like a loader) while waiting for the lazy-loaded component.
💡 Best Practices
- Use lazy loading for routes and large components.
- Combine with dynamic imports for better code organization.
- Use Lighthouse or Webpack Bundle Analyzer to track bundle size.
Popular Posts
📘 Day 36: Understanding the DOM (Document Object Model)
- Get link
- X
- Other Apps
Day 27: Bootstrap Mini Project – Responsive Portfolio Page
- Get link
- X
- Other Apps
Comments
Post a Comment