简体   繁体   English

react-router 4不会让我重定向回首页吗?

[英]react-router four won't let me redirect back to home?

I have something like this using react-router 4: 我使用react-router 4有这样的东西:

    <Route exact path="/" children={({match}) => (
      <TransitionGroup component={firstChild}>
        {match && <Home match={match} />}
      </TransitionGroup>
    )}/>
    <Route exact path="/idea/:id" children={({match}) => (
      <TransitionGroup component={firstChild}>
        {match && <IdeaDetails match={match} />}
      </TransitionGroup>
    )}/>
    <Redirect from='*' to='/' />

Basically if someone types in a url other than idea/:id , I want to redirect them back to the home page. 基本上,如果有人输入了除idea/:id以外的其他网址,我想将他们重定向回首页。 When I run this code, it seems to do I expect. 当我运行此代码时,似乎可以预期。 Except in the developer consoles, I see this warning 除了在开发人员控制台中,我看到此警告

Warning: You tried to redirect to the same route you're currently on: "/" 警告:您试图重定向到当前使用的相同路由:“ /”

Is there a way to define the routes that I have without tripping this warning? 有没有一种方法可以定义我拥有的路线而不会触发此警告? Is there a way to tell react router to not process any subsequent route rules once one of them has been found? 一旦找到其中一个路由规则,是否有办法告诉React路由器不要处理任何后续路由规则?

Is there a way to tell react router to not process any subsequent route rules once one of them has been found? 一旦找到其中一个路由规则,是否有办法告诉React路由器不要处理任何后续路由规则?

Yes, you need to wrap your routes in a Switch component . 是的,您需要将路由包装在Switch组件中 When your routes are wrapped in a Switch , it finds the FIRST matching route and then returns it without continuing. 当您的路由包装在Switch ,它将找到第一条匹配的路由,然后不继续进行返回。 Without a Switch , the router will return ALL routes that match the current url. 如果没有Switch ,路由器将返回与当前URL匹配的所有路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM