当路径不正确时,我尝试使用React路由器v4渲染NotFound组件或重定向到根路由,但是当我在" /"我的应用程序呈现2个组件。
<Switch>
<Navbar>
<Route exact path="/" component={App} />
<Route path="/Other" component={Other} />
<Route component={NotFound} />
</Navbar>
</Switch>
我的Navbar看起来像:
render()
{
return(
<div>
{/*Any content here*/}
{this.props.children}
</div>
);
}
如果我将<Route component={NotFound} />
替换为<Redirect to="/" />
,它看起来有效,但我收到错误:Warning: You tried to redirect to the same route you're currently on: "/"
感谢您的帮助! :)
答案 0 :(得分:1)
您的Switch
组件的展示位置不正确,它应该将组件子项包装在Routes
定义的位置
<Navbar>
<Switch>
<Route exact path="/" component={App} />
<Route path="/Other" component={Other} />
<Route component={NotFound} />
</Switch>
</Navbar>