英文:
React routing renders two urls at the same time
问题
I have a simple routing that looks like this:
<Switch>
<div className="privateContent">
<Route exact path="/games/new">
<PrivateRoute isLogged={loggedIn}>
<CreateGame />
</PrivateRoute>
</Route>
<Route exact path="/games/:id">
<PrivateRoute isLogged={loggedIn}>
<ShowGame />
</PrivateRoute>
</Route>
</div>
</Switch>
My react routing version is 5.2.0
, and according to what I have read about it, when using Switch
, it should render the FIRST path it matches. HOWEVER, when I try to reach games/new
, BOTH of <ShowGame />
and <CreateGame />
get rendered.
How to fix this?
英文:
i have a simple routing that looks like this:
<Switch>
<div className="privateContent">
<Route exact path="/games/new">
<PrivateRoute isLogged={loggedIn}>
<CreateGame />
</PrivateRoute>
</Route>
<Route exact path="/games/:id">
<PrivateRoute isLogged={loggedIn}>
<ShowGame />
</PrivateRoute>
</Route>
my react routing version is 5.2.0
and according to what i have read about it, when using Switch
it should render the FIRST path it matches, HOWEVER, when i try to reach games/new
BOTH of <ShowGame />
and <CreateGame />
get rendered
How to fix this?
答案1
得分: 1
问题出在所有路由之上的 <div className="privateContent">
- 当所有的
将这个 <div>
放在 <Switch>
之上,它应该能正常工作。
英文:
The problem is the <div className="privateContent">
above all routes - <Switch>
works as expected when all <Route>
s are direct children of <Switch>
.
Put this div
above <Switch>
and it should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论