<Redirect>

渲染<Redirect> 的时候将会导航到一个新的地址(location)。这个新的地址(location)将会覆盖在访问历史记录里面的原地址,就像服务端的重定向(HTTP 3XX)一样。

  1. import { Route, Redirect } from 'react-router'
  2. <Route exact path="/" render={() => (
  3. loggedIn ? (
  4. <Redirect to="/dashboard"/>
  5. ) : (
  6. <PublicHomePage/>
  7. )
  8. )}/>

to: string

重定向目标URL。

  1. <Redirect to="/somewhere/else"/>

to: object

重定向目标地址(location)。

  1. <Redirect to={{
  2. pathname: '/login',
  3. search: '?utm=your+face',
  4. state: { referrer: currentLocation }
  5. }}/>

push: bool

当设置为 true 时,重定向(redirecting)将会把新地址加入访问历史记录里面,而不是替换掉目前的地址。

  1. <Redirect push to="/somewhere/else"/>

from: string

需要被重定向的路径(pathname)。当渲染一个包含在<Switch>里面的<Redirect>的时候,这可以用作匹配一个地址(location)。

  1. <Switch>
  2. <Redirect from='/old-path' to='/new-path'/>
  3. <Route path='/new-path' component={Place}/>
  4. </Switch>