Skip to content

Commit

Permalink
add lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
toocozzy committed Feb 18, 2022
1 parent 8c45cfb commit e7f9505
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useEffect } from "react";
import React, { useEffect, Suspense } from "react";
import { Route, Routes, Navigate } from "react-router-dom";
import Home from "./pages/Home";
import Checkout from "./pages/Checkout";
import { useDispatch, useSelector } from "react-redux";
import { getCartData, sendCartData } from "./store/cart-actions";
import NotFound from "./pages/NotFound";
import LoadingSpinner from "./components/UI/LoadingSpinner";
import Home from "./pages/Home";

const Checkout = React.lazy(() => import("./pages/Checkout"));
const NotFound = React.lazy(() => import("./pages/NotFound"));

let isInitial = true;

Expand All @@ -26,12 +28,14 @@ function App() {
}, [cart, dispatch]);

return (
<Routes>
<Route path="/" element={<Navigate to="/home" />} />
<Route path="/home" element={<Home />} />
<Route path="/checkout" element={<Checkout />} />
<Route path="*" element={<NotFound />} />
</Routes>
<Suspense fallback={<LoadingSpinner />}>
<Routes>
<Route path="/" element={<Navigate to="/home" />} />
<Route path="/home" element={<Home />} />
<Route path="/checkout" element={<Checkout />} />
<Route path="*" element={<NotFound />} />
</Routes>
</Suspense>
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/Items/ItemsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ const ItemsList = () => {
if (isLoading) {
return (
<section className={styles["items-section"]}>
<p className={styles.loading}>
<div className={styles.loading}>
Loading <span className="loading dots2"></span>{" "}
</p>
</div>
</section>
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/components/UI/LoadingSpinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import styles from "./LoadingSpinner.module.css";

const LoadingSpinner = () => {
return (
<div>
<p className={styles.loading}>
Loading <span className="loading dots2"></span>
</p>
</div>
);
};

export default LoadingSpinner;
6 changes: 6 additions & 0 deletions src/components/UI/LoadingSpinner.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

0 comments on commit e7f9505

Please sign in to comment.