【发布时间】:2022-01-17 20:19:30
【问题描述】:
目前使用react-router-dom6.1.1,我正在使用private route。
在这个private route 里面,我通常有其他路线(这样我就可以在上面保留我的Sidebar)。
我的代码是这样的
// App.tsx
const RequireAuth: React.FC<PrivateRouteProps> = ({ children, redirectTo }) => {
const isAuthenticated = Auth.isLogedIn()
return isAuthenticated ? children : <Navigate to={redirectTo} />
}
const Page = () => {
return (
<div className={css.host}>
<BrowserRouter>
<Routes>
<Route path="/login" element={<Login />} />
<Route
path="/"
element={
<RequireAuth redirectTo="/login">
<Home />
</RequireAuth>
}
/>
</Routes>
</BrowserRouter>
</div>
)
}
// Home/index.tsx
const Home = () => {
return (
<div className={css.host}>
<Sidebar sections={sidebarOptions(t)} />
<Routes>
{routes.map(({ breadCrumbtitle, link, component }, index) => (
<Route path={link} key={index}>
{component ? component : <p>[{breadCrumbtitle}] To be done</p>}
</Route>
))}
</Routes>
</div>
)
}
所以...此设置适用于v5,但它似乎不适用于v6。
如果我在登录后仍想为所有路由保留Sidebar,我该怎么办?
【问题讨论】:
标签: javascript reactjs react-router react-router-dom