[英]Nested Routing in React Router Dom V6
I need two sets of routing in my app.我的应用程序中需要两组路由。 One at the top level which routes between the different pages that don't need authentication and another set inside the authenticated area.
一个在顶层,它在不需要身份验证的不同页面之间路由,另一个在经过身份验证的区域内设置。 Here's my top level route component.
这是我的顶级路由组件。
function App() {
return (
<Router>
<Routes>
<Route path='/' element={<ProtectedRoute/>}>
<Route path='/' element={<Dashboard>
</Dashboard>}/>
</Route>
<Route exact path="/sign-up" element={<SignUpPage/>}/>
<Route exact path="/login" element={<LogInPage/>}/>
<Route exact path="/activate-user" element={<ActivateUserPage/>}/>
<Route exact path="/forgot-password" element={<ForgotPasswordPage/>}/>
<Route exact path="/reset-password" element={<ResetPasswordPage/>}/>
</Routes>
</Router>
);
}
export default App;
This is the protected route.这是受保护的路线。
export const ProtectedRoute = () => {
const auth = localStorage.getItem("xyz");
return auth ? <Dashboard /> : <Navigate to="/login" />
}
This part works.这部分有效。 Now I need a routing inside the Dashboard.
现在我需要仪表板内的路由。 Here's my dashboard component with the desired routing.
这是我的仪表板组件,其中包含所需的路由。
const Dashboard = () => {
return (
<nav>
<ul>
<li><Link to=""><AiOutlineHome/></Link></li>
<li><Link to=""><AiOutlineHistory/></Link></li>
<li><Link to=""><BsFillPlusCircleFill/></Link></li>
<li><Link to=""><BsChatDots/></Link></li>
<li><Link to=""><RiUserSettingsLine/></Link></li>
</ul>
</nav>
<section>
<Route path='/feeds' element={<HomePage/>}/>
<Route path='/capture' element={<CaptureStoryPage/>}/>
</section>
</div>
)
}
This doesn't work and the error that I get is this:这不起作用,我得到的错误是:
router.ts:5 Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.
I'm new to React and could really use some help here.我是 React 的新手,真的可以在这里使用一些帮助。 Thanks in advance.
提前致谢。
Hopefully you meant for the ProtectedRoute
to render an Outlet
instead of the Dashboard
component:希望您打算让
ProtectedRoute
呈现Outlet
而不是Dashboard
组件:
export const ProtectedRoute = () => {
const auth = localStorage.getItem("xyz");
return auth ? <Outlet /> : <Navigate to="/login" replace />;
}
If you need to render descendent routes in the Dashboard
component then you need two things.如果您需要在
Dashboard
组件中呈现后代路由,那么您需要做两件事。
Route
rendering Dashboard
needs to append the "*"
wildcard matcher to its route path so descendent routes can be matched. Route
渲染Dashboard
需要将"*"
通配符匹配器附加到其路由路径,以便可以匹配后代路由。Route
components must be rendered by a Routes
component or parent Route
component, they can't be rendered directly.Route
组件必须由Routes
组件或父Route
组件渲染,它们不能直接渲染。 Example:例子:
function App() {
return (
<Router>
<Routes>
<Route element={<ProtectedRoute />}>
<Route path='/*' element={<Dashboard />} /> // <-- match descendent routes
</Route>
<Route path="/sign-up" element={<SignUpPage />} />
<Route path="/login" element={<LogInPage />} />
<Route path="/activate-user" element={<ActivateUserPage />} />
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
<Route path="/reset-password" element={<ResetPasswordPage />} />
</Routes>
</Router>
);
}
... ...
const Dashboard = () => {
return (
<div>
<nav>
<ul>
<li><Link to=""><AiOutlineHome/></Link></li>
<li><Link to=""><AiOutlineHistory/></Link></li>
<li><Link to=""><BsFillPlusCircleFill/></Link></li>
<li><Link to=""><BsChatDots/></Link></li>
<li><Link to=""><RiUserSettingsLine/></Link></li>
</ul>
</nav>
<section>
<Routes> // <-- wrap routes in `Routes`
<Route path='/feeds' element={<HomePage/>}/>
<Route path='/capture' element={<CaptureStoryPage/>}/>
</Routes>
</section>
</div>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.