Lazy loading in React (2024)

Create a separate file for the component you want to lazy load

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

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

export default Products;

Use React.lazy() to import the component dynamically in your route file

const Prodcuts = React.lazy(() => import("./components/products/Products"));
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
// import Prodcuts from "./components/products/Products";
import PageNotFound from "./components/PagenotFound/PageNotFound";
import {
createBrowserRouter,
createRoutesFromElements,
Route,
RouterProvider,
} from "react-router-dom";
import Home from "./components/Home/Home";

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

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

const root = ReactDOM.createRoot(document.getElementById("root"));

In the code above, the product component is loaded lazily using React. lazy(). The Suspense component is used to specify a fallback UI while the lazy-loaded component is being loaded.

Run your application:

When you navigate to the /products route, the ProductsComponent will be loaded only when needed.

Note: The fallback prop of Suspense is required. It defines the UI that will be displayed while the component is being loaded. You can customize this UI based on your application’s design and requirements.

Thank you !!!

Lazy loading in React (2024)
Top Articles
Upload files & folders to Google Drive - Computer
Post-Holiday Spending: What Do Consumers Buy In January?  - Capstone Logistics
Wisconsin Women's Volleyball Team Leaked Pictures
Aadya Bazaar
Mileage To Walmart
Otterbrook Goldens
Kostenlose Games: Die besten Free to play Spiele 2024 - Update mit einem legendären Shooter
Citi Card Thomas Rhett Presale
Alaska Bücher in der richtigen Reihenfolge
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
Walgreens On Nacogdoches And O'connor
Little Rock Arkansas Craigslist
Culvers Tartar Sauce
Skylar Vox Bra Size
Watch TV shows online - JustWatch
The Murdoch succession drama kicks off this week. Here's everything you need to know
24 Hour Walmart Detroit Mi
Bcbs Prefix List Phone Numbers
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
979-200-6466
Driving Directions To Bed Bath & Beyond
Nhl Tankathon Mock Draft
Tu Pulga Online Utah
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Uncovering The Mystery Behind Crazyjamjam Fanfix Leaked
Disputes over ESPN, Disney and DirecTV go to the heart of TV's existential problems
Myql Loan Login
Hwy 57 Nursery Michie Tn
Jackass Golf Cart Gif
Redding Activity Partners
Syracuse Jr High Home Page
Of An Age Showtimes Near Alamo Drafthouse Sloans Lake
Craigslist Albany Ny Garage Sales
Staar English 1 April 2022 Answer Key
دانلود سریال خاندان اژدها دیجی موویز
How are you feeling? Vocabulary & expressions to answer this common question!
Evil Dead Rise (2023) | Film, Trailer, Kritik
2007 Peterbilt 387 Fuse Box Diagram
Firestone Batteries Prices
Tunica Inmate Roster Release
How I Passed the AZ-900 Microsoft Azure Fundamentals Exam
Gas Buddy Il
White County
Random Animal Hybrid Generator Wheel
Movie Hax
Minecraft: Piglin Trade List (What Can You Get & How)
Shannon Sharpe Pointing Gif
Billings City Landfill Hours
Fahrpläne, Preise und Anbieter von Bookaway
Provincial Freeman (Toronto and Chatham, ON: Mary Ann Shadd Cary (October 9, 1823 – June 5, 1893)), November 3, 1855, p. 1
Dinargurus
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6544

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.