React Router with protected routes

13442
0

React Router v6 provides declarative routing with data loading and authentication guards. I wrap protected routes in a ProtectedRoute component that checks authentication state from context and redirects to login if needed. The Navigate component handles redirects while preserving the attempted URL so users land where they intended after logging in. Nested routes share layouts via Outlet, avoiding component duplication. The useNavigate hook enables programmatic navigation after form submissions. Lazy loading route components with React.lazy and Suspense keeps initial bundle sizes small. This routing setup handles complex multi-page apps while maintaining type safety with TypeScript.