Overview
The application uses React Router v6 for client-side routing with lazy-loaded pages for optimal performance. All routing configuration is centralized inApp.tsx.
Router Configuration
Base Setup
Location:src/App.tsx
Route Structure
Main Routes
Route Patterns
Catalog Routes
Paginated Catalog:- URL:
/page/1,/page/2, etc. - Shows all content types
- Dynamic page number from URL parameter
- URL:
/peliculas,/series - Filters content by section
- Section passed as prop to CatalogPage
Detail Routes
Series Detail:- URL:
/series/breaking-bad - Shows series information and episodes
- Slug extracted from URL parameter
- URL:
/movie/inception - Shows movie information and player
- Slug extracted from URL parameter
- URL:
/anime/one-piece - Shows anime information and episodes
- Slug extracted from URL parameter
Player Route
- URL:
/ver/serie/breaking-bad,/ver/pelicula/inception - Dynamic content type (serie/pelicula/anime)
- Dynamic content slug
- Displays video player
Lazy Loading
Implementation
Pages are lazy loaded using React’slazy() function:
Suspense Fallback
A loading indicator is shown while pages load:Benefits
- Faster Initial Load: Only loads the current page’s code
- Code Splitting: Each page is a separate bundle
- Better Performance: Reduces initial JavaScript bundle size
- User Experience: Shows loading state during page transitions
Navigation
NavLink Component
The Layout component usesNavLink for active route styling:
Link Component
For dynamic navigation (e.g., catalog items):Navigate Component
For programmatic redirects:Route Parameters
Accessing Parameters
Use theuseParams hook to access URL parameters:
Multiple Parameters
Location State
Using useLocation
Access current location information:Programmatic Navigation
useNavigate Hook
Navigate programmatically in response to events:Route Guards
While not implemented in the current app, route guards can be added:Scroll Restoration
React Router automatically handles scroll position. For custom behavior:Best Practices
- Lazy Loading: Use
React.lazy()for all route components - Suspense Boundaries: Always wrap lazy routes in
Suspense - Error Boundaries: Wrap routes in
ErrorBoundaryfor error handling - Type Safety: Use TypeScript generics with
useParams - SEO: Use descriptive slugs in URLs
- Redirects: Use
Navigatecomponent withreplaceprop for redirects - Active Links: Use
NavLinkwithendprop for exact matching - Route Organization: Keep route definitions in one place (App.tsx)
Route Hierarchy
Adding New Routes
To add a new route:- Create the page component in
src/pages/ - Lazy load it in
App.tsx - Add the route definition
- Update navigation if needed
- Test route parameters and navigation