Skip to content

Commit

Permalink
lazy() & Suspense refatoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danretegan committed Feb 14, 2024
1 parent 1137f12 commit 01b47fd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { lazy } from 'react';
import { Route, Routes } from 'react-router-dom';

const Home = lazy(() => import('../pages/Home'));
const Movies = lazy(() => import('../pages/Movies'));
const MovieDetails = lazy(() => import('../pages/movieDetails/MovieDetails'));
import Home from '../pages/Home';
import Movies from '../pages/Movies';
import MovieDetails from '../pages/movieDetails/MovieDetails';
import SharedLayout from './sharedLayout/SharedLayout';
import NotFoundPage from '../pages/NotFoundPage';

// Importurile dinamice (lazy()) trebuie facute DUPA importurile statice!

const Cast = lazy(() => import('../pages/cast/Cast'));
const Reviews = lazy(() => import('../pages/Reviews'));
const SharedLayout = lazy(() => import('./sharedLayout/SharedLayout'));
const NotFoundPage = lazy(() => import('../pages/NotFoundPage'));

const App = () => {
return (
Expand Down
2 changes: 2 additions & 0 deletions src/components/sharedLayout/SharedLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ const SharedLayout = () => {
);
};

// Dacă folosim tehnica «shared layout», atunci Suspense trebuie plasat direct în componenta SharedLayout. În caz contrar, la fiecare încărcare a paginii, componentele comune a paginilor, cum ar fi header și navigation, vor dispărea și vor fi randate din nou.

export default SharedLayout;
6 changes: 2 additions & 4 deletions src/pages/movieDetails/MovieDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Link, Outlet, useNavigate, useParams } from 'react-router-dom';
import { fetchMovieDetails } from '../../services/api';
import styles from './MovieDetails.module.css';
Expand Down Expand Up @@ -78,9 +78,7 @@ const MovieDetails = () => {
</Link>
</nav>
<hr />
<Suspense fallback={<div>Loading...</div>}>
<Outlet />
</Suspense>
<Outlet />
</div>
);
};
Expand Down

0 comments on commit 01b47fd

Please sign in to comment.