Lazy Loading in React
Joodi

Joodi @joodi

About: 𝗙𝗿𝗼𝗻𝘁-𝗲𝗻𝗱 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 sharing what I learn. I introduce new tools and ideas here—your support means the world! 🚀

Joined:
Mar 2, 2024

Lazy Loading in React

Publish Date: Apr 15
22 7

Image description

Lazy Loading in React: Improving Performance with Dynamic Imports

Lazy loading in React allows you to optimize the performance of your application by splitting the code into smaller chunks. Instead of loading the entire application upfront, lazy loading loads only the code that's needed for a particular part of the app when it's actually required. This can significantly improve the initial loading time of your application.

React provides two main features to implement lazy loading: React.lazy() for dynamic imports and React.Suspense to handle loading states.


⚡ What is Lazy Loading in React?

Lazy loading helps break your application into smaller pieces (chunks) so that the code is only loaded when it’s required. React provides the ability to dynamically import components only when they’re about to be rendered.

Why is it Important?

  • Improved Initial Load Time: By loading only the code that is needed, you reduce the initial load time of your application.
  • Better User Experience: It provides a faster initial render and a smoother experience, especially in large applications.

🧩 How Does Lazy Loading Work in React?

React uses React.lazy() to dynamically import components. This function allows you to load a component only when it is needed. However, React does not automatically handle the loading state; that's where React.Suspense comes in. Suspense lets you specify a fallback UI that will be shown while the component is loading.

Step-by-Step Example

1. Create a Component to Lazy Load

// Products.js
import React from 'react';

const Products = () => {
  return <div>This is a lazy-loaded component!</div>;
};

export default Products;
Enter fullscreen mode Exit fullscreen mode

2. Use React.lazy() to Dynamically Import the Component

const Products = React.lazy(() => import("./components/products/Products"));
Enter fullscreen mode Exit fullscreen mode

3. Wrap the Lazy-Loaded Component with Suspense

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import PageNotFound from "./components/PagenotFound/PageNotFound";
import { createBrowserRouter, createRoutesFromElements, Route, RouterProvider } from "react-router-dom";
import Home from "./components/Home/Home";

const Products = React.lazy(() => import("./components/products/Products"));

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<App />}>
      <Route path="/" element={<Home />} />
      <Route
        path="products"
        element={
          <React.Suspense fallback={<div>Loading...</div>}>
            <Products />
          </React.Suspense>
        }
      />
      <Route path="*" element={<PageNotFound />} />
    </Route>
  )
);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<RouterProvider router={router} />);
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The Products component is dynamically imported using React.lazy().
  • The Suspense component is used to display a fallback UI (<div>Loading...</div>) while the Products component is being loaded.

4. Navigate to the Route to See Lazy Loading in Action

Once the user navigates to the /products route, the Products component will be fetched and rendered only when required. This reduces the initial payload size and improves the performance.


⚠️ Important Notes

  • fallback prop in Suspense: The fallback prop is mandatory in the Suspense component. It specifies the UI to show while the lazy-loaded component is being fetched. You can customize it according to your needs, such as displaying a loading spinner or a skeleton screen.
  • Multiple Lazy-Loaded Components: You can use React.lazy() and Suspense in various parts of your app to implement lazy loading for multiple components.

🚀 Conclusion

Lazy loading is a simple yet powerful technique that can drastically improve your React app's performance. With React.lazy() and React.Suspense, you can efficiently manage when components are loaded, which helps reduce the initial loading time and enhances the overall user experience.

Start using lazy loading today and see the difference in performance for yourself!

Comments 7 total

  • Sanjay Joshi
    Sanjay JoshiApr 15, 2025

    Nice

    • Joodi
      JoodiApr 15, 2025

      Means a lot, thank you!

  • Nevo David
    Nevo DavidApr 15, 2025

    Neat way to make apps faster using lazy loading in React!

    • Joodi
      JoodiApr 16, 2025

      Absolutely! Lazy loading is such an effective way to improve app performance by loading components only when needed.

  • Badejo Emmanuel Adewale
    Badejo Emmanuel AdewaleApr 17, 2025

    Insightful
    This can make a significant increase in the performance of an application

    • Joodi
      JoodiApr 17, 2025

      Thanks! Yeah exactly, lazy loading really helps boost performance, especially when there are lots of components or pages that don’t need to load right away.

  • Venkatesh Kasani
    Venkatesh KasaniApr 20, 2025

    Clean explanation.

Add comment