https://www.robinwieruch.de/react-router-nested-routes
https://www.positronx.io/react-js-nested-routes-us...
import {BrowserRouter as Router, Routes, Route, Navigate} from 'react-router-dom';
function App() {
const {getUser, user, setUser} = useUser();
return (
<Router>
<Routes>
<Route path="/" element={<LoginForm user={user} getUser={getUser}/>}/>
<Route path="login" element={<LoginForm user={user} getUser={getUser}/>}/>
<Route path="dashboard/*" element={<PrivateRoute user={user}><DashboardRoutes user={user}/></PrivateRoute>}/>
</Routes>
</Router>
);
}
export default App;
const DashboardRoutes = ({user}) => (
<Routes>
<Route path="/" element={<Dashboard user={user}/>}/>
<Route path="acts" element={<ActsPage user={user}/>}/>
</Routes>
);
const PrivateRoute = ({user, children}) => {
if(localStorage.getItem('token')) {
return children;
}
return <Navigate to="/login"/>;
}
В
DashboardRoutes()
можно добавлять вложенные роуты.