Skip to content

Commit 2349b97

Browse files
committed
Lazy/On Demand Loading
1 parent 78f1cb2 commit 2349b97

4 files changed

Lines changed: 57 additions & 20 deletions

File tree

App.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import React from "react";
1+
import React, { lazy, Suspense } from "react";
22
import ReactDOM from "react-dom/client";
33
import Body from "./src/components/Body";
44
import Header from "./src/components/Header";
55
import Footer from "./src/components/Footer";
6-
import About from "./src/components/About";
7-
import ContactUs from "./src/components/ContactUs";
86
import Cart from "./src/components/Cart";
97
import Error from "./src/components/Error";
108
import { createBrowserRouter, RouterProvider, Outlet } from "react-router-dom";
119
import RestaurantMenu from "./src/components/RestaurantMenu";
12-
import Profile from "./src/components/Profile";
10+
// import Profile from "./src/components/Profile";
11+
// import About from "./src/components/About";
12+
// import ContactUs from "./src/components/ContactUs";
13+
14+
const Profile = lazy(() => import("./src/components/Profile"));
15+
const ContactUs = lazy(() => import("./src/components/ContactUs"));
16+
const About = lazy(() => import("./src/components/About"));
1317

1418
const AppLayout = () => {
1519
return (
@@ -34,17 +38,29 @@ const appRouter = createBrowserRouter([
3438
},
3539
{
3640
path: "/about",
37-
element: <About />,
41+
element: (
42+
<Suspense fallback={<h1>Loading...</h1>}>
43+
<About />
44+
</Suspense>
45+
),
3846
children: [
3947
{
4048
path: "profile",
41-
element: <Profile />,
49+
element: (
50+
<Suspense>
51+
<Profile />
52+
</Suspense>
53+
),
4254
},
4355
],
4456
},
4557
{
4658
path: "/contactUs",
47-
element: <ContactUs />,
59+
element: (
60+
<Suspense>
61+
<ContactUs />
62+
</Suspense>
63+
),
4864
},
4965
{
5066
path: "/cart",

src/components/Body.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@ import { useState, useEffect } from "react";
44
import { Shimmer } from "./Shimmer";
55
import { Link } from "react-router-dom";
66
import { filterData } from "../utils/Helper";
7-
8-
// function filterData(searchText, restaurant) {
9-
// const filterData = restaurant.filter((restaurants) =>
10-
// restaurants?.data.name?.toLowerCase().includes(searchText.toLowerCase())
11-
// );
12-
// return filterData;
13-
// }
7+
import useOnline from "../utils/useOnline";
148

159
const Body = () => {
1610
const [filteredRestaurant, setFilteredRestaurant] = useState([]);
1711
const [allRestaturants, setAllRestaurants] = useState([]);
1812
const [searchText, setSearchText] = useState("");
19-
// console.log("Consoling");
2013

2114
useEffect(() => {
2215
getRestaurants();
@@ -35,6 +28,14 @@ const Body = () => {
3528
setAllRestaurants(json?.data?.success?.cards[0]?.favourite?.cards);
3629
}
3730

31+
const isOnline = useOnline();
32+
if (!isOnline)
33+
return (
34+
<h1>
35+
🔴 OOPS ! Something went wrong, Please check your internet connection
36+
</h1>
37+
);
38+
3839
//Not rendering the component (Early Return)
3940
if (!allRestaturants) {
4041
return null;

src/components/RestaurantMenu.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ const RestaurantMenu = () => {
88

99
const [restaurant, setRestaurant] = useState(null);
1010
const [menuItems, setmenuItems] = useState(null);
11-
// const useEffect=(()=>{
12-
// const {restaurantInfo,menu}=
13-
// },restaurant)
1411

12+
1513
useEffect(() => {
1614
getRestaurants();
1715
}, []);
@@ -78,7 +76,6 @@ const RestaurantMenu = () => {
7876
alt="foodImage"
7977
/>
8078
)}
81-
8279
<h5>{insideMenu?.name}</h5>
8380
<h4>{insideMenu?.price / 100}</h4>
8481
</div>
@@ -93,4 +90,3 @@ const RestaurantMenu = () => {
9390
};
9491
export default RestaurantMenu;
9592

96-
// map/card/info

src/utils/useOnline.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useState, useEffect } from "react";
2+
const useOnline = () => {
3+
const [isOnline, setIsOnline] = useState(true);
4+
5+
useEffect(() => {
6+
const handleOnline = () => {
7+
setIsOnline(true);
8+
};
9+
const handleOffline = () => {
10+
setIsOnline(false);
11+
};
12+
13+
document.addEventListener("online", handleOnline);
14+
document.addEventListener("offline", handleOffline);
15+
16+
return () => {
17+
window.removeEventListener("online", handleOnline);
18+
window.removeEventListener("offline", handleOffline);
19+
};
20+
}, []);
21+
22+
return isOnline; //Returns True/False
23+
};
24+
export default useOnline;

0 commit comments

Comments
 (0)