[英]React Router 4 and exact path with dynamic param
I have issues with component being displayed on every single path because React Router 4 isn't using exact for that route (or so it appears). 我在每条路径上都显示组件有问题,因为React Router 4没有对该路由使用确切的路径(或因此出现)。
<Route path="/" exact component={Explore} />
<Route path="/about" component={About} />
// The one making problems
<Route
path="/:username"
exact={true}
render={props => <Profile {...props} />}
/>
So when I go to http://example.com/about , my Profile component is still being rendered. 因此,当我转到http://example.com/about时 ,我的Profile组件仍在呈现中。 I guess the problem is in the route as it expects param
:username
and that goes right after /
(root). 我猜问题出在路由中,因为它期望param
:username
,并且紧跟在/
(根)之后。 Am I doing something wrong? 难道我做错了什么? I could add another route for
/:username
, like /profile/:username
, but I'd like to keep it the way it is, if it's possible. 我可以为
/:username
添加另一条路由,例如/profile/:username
,但我希望尽可能保持原样。
Assuming that you are not using Switch. 假设您没有使用Switch。 Switch will solve your problem because it will only render the first path that matches.
Switch将解决您的问题,因为它只会呈现匹配的第一个路径。
<Switch>
<Route path="/about" component={About}/>
<Route path="/:username" render={props => <Profile {...props} />} />
<Route path="/" component={Explore} />
<Route component={NotFoundPage} />
</Switch>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.