Mbr*_*vda 5
你不能 Route
在 React Router v6 中编写 s。相反,在与其他路由相同的级别创建 Route,并在element
prop 中使用您的 PrivateRoute 组件。示例(来源):
import { Routes, Route, Navigate } from "react-router-dom";
function App() {
return (
<Routes>
<Route path="/public" element={<PublicPage />} />
<Route
path="/protected"
element={
// Good! Do your position here instead of wrapping <Route>.
// This is really just inverting the wrapping, but it's a lot
// more clear which ponents expect which props.
<RequireAuth redirectTo="/login">
<ProtectedPage />
</RequireAuth>
}
/>
</Routes>
);
}
function RequireAuth({ children, redirectTo }) {
let isAuthenticated = getAuth();
return isAuthenticated ? children : <Navigate to={redirectTo} />;
}
更多推荐
路由,React,Router
发布评论