[英]A route is not covered in layout component, but still renders layout in React
I'm having to trouble making a route without layout component.我不得不在没有布局组件的情况下制作路线。 Please check my code first.
请先检查我的代码。
// App.js
const App = () => {
return (
<div className={styles.container}>
<Header />
<main>
<QuizBoxContainer>
<Routes>
<Route path='/' element={<QuizSelect />} />
<Route path='quiz-for/:language' element={<QuizCard />} />
<Route path='result' element={<ResultPage />} />
</Routes>
</QuizBoxContainer>
<Routes>
<Route path='wrong-answer' element={<WrongAnswer />} />
</Routes>
</main>
</div>
)
}
In the code, <QuizBoxContainer>
is the layout component.在代码中,
<QuizBoxContainer>
是布局组件。
What I was trying to do is making another path,我想做的是另辟蹊径,
<Routes>
<Route path='wrong-answer' element={<WrongAnswer />} />
</Routes>
outside of <QuizBoxContainer>
so I can use another layout on this component. <QuizBoxContainer>
之外,所以我可以在这个组件上使用另一个布局。
However, when I go to WrongAnswer,然而,当我去 WrongAnswer 时,
It still renders.它仍然呈现。
What is wrong with my code, and how can I solve it?我的代码有什么问题,我该如何解决?
The is because the QuizBoxContainer
layout wrapper component is not rendered on any route, it's always rendered.这是因为
QuizBoxContainer
布局包装器组件不会在任何路由上呈现,它总是被呈现。
Move QuizBoxContainer
into a layout route .将
QuizBoxContainer
移动到布局路径中。 You'll need to update QuizBoxContainer
so it renders an Outlet
component instead of the children
prop.您需要更新
QuizBoxContainer
以便它呈现一个Outlet
组件而不是children
道具。
Example:例子:
import { Outlet } from 'react-router-dom';
const QuizBoxContainer = () => {
...
return (
... quiz container layout/styling ...
<Outlet /> // <-- nested routes render content here
...
);
};
Render QuizBoxContainer
on a layout route wrapping the routes you want to render within it, render the wrong answer route outside the layout route.在包含要在其中渲染的路由的布局路由上渲染
QuizBoxContainer
,在布局路由之外渲染错误的答案路由。
const App = () => {
return (
<div className={styles.container}>
<Header />
<main>
<Routes>
<Route element={<QuizBoxContainer />}>
<Route path='/' element={<QuizSelect />} />
<Route path='quiz-for/:language' element={<QuizCard />} />
<Route path='result' element={<ResultPage />} />
</Route>
<Route path='wrong-answer' element={<WrongAnswer />} />
</Routes>
</main>
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.