Skip to main content

Command Palette

Search for a command to run...

πŸ“˜ React Mastery Series β€” Day 25

Updated
β€’3 min read
πŸ“˜ React Mastery Series β€” Day 25
S

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.

Code Splitting & Lazy Loading β€” Make Your React App Faster πŸš€

Have you ever noticed:

πŸ‘‰ Some apps load instantly
πŸ‘‰ Others take time and feel heavy

Why?

Because of bundle size.

By default, React bundles your entire app into one large file.

That means:

  • More load time

  • Slower performance

  • Bad user experience

Today we fix this using:

βœ” Code Splitting
βœ” Lazy Loading


πŸ€” What is Code Splitting?

Code Splitting is the process of breaking your app into smaller chunks so they load only when needed.

Instead of loading everything at once…

πŸ‘‰ Load only what the user needs.


🧠 What is Lazy Loading?

Lazy Loading means loading components only when they are required.

For example:

  • Login page β†’ load login code

  • Dashboard β†’ load dashboard code later


πŸ›  How to Implement Lazy Loading

React provides:

πŸ‘‰ React.lazy()
πŸ‘‰ Suspense


βœ… Step 1 β€” Use React.lazy

import React, { lazy } from "react";

const Dashboard = lazy(() => import("./Dashboard"));

βœ… Step 2 β€” Wrap with Suspense

import { Suspense } from "react";

function App() {
  return (
    <Suspense fallback={<p>Loading...</p>}>
      <Dashboard />
    </Suspense>
  );
}

πŸ”Ž What’s Happening?

  • Dashboard loads only when needed

  • Until then β†’ "Loading..." is shown

  • Improves initial load time


πŸ”₯ Real-World Example β€” Route-Based Splitting

const Home = lazy(() => import("./Home"));
const Profile = lazy(() => import("./Profile"));
const Settings = lazy(() => import("./Settings"));

Each page loads separately β†’ faster app πŸš€


🎯 Why It Matters

βœ” Faster initial load

βœ” Better performance

βœ” Improved user experience

βœ” Smaller bundle size


⚠ Important Notes

βœ” Always wrap lazy components in Suspense

βœ” Provide meaningful fallback UI

βœ” Use for large components or pages


🧠 One-Liner

Code splitting divides the app into smaller chunks, and lazy loading loads components only when needed using React.lazy and Suspense.


πŸ”— How It Connects

βœ” Performance Optimization (Day 13, 14)

βœ” Large Applications

βœ” Routing (React Router)

βœ” Real-world production apps


βœ… Key Takeaways

βœ” Improves app performance

βœ” Reduces bundle size

βœ” Loads components on demand

βœ” Essential for scalable apps


πŸ”œ Coming Next

Day 26 β€” Higher Order Components (HOC) Simplified

Now we revisit an important reusable pattern πŸ”₯

More from this blog