简体   繁体   English

如何使用react路由器将重定向重写为普通路由?

[英]How do I rewrite redirects to plain routes using react router?

Using react 15.1.0 and react-router 2.5.2 , I wish to rewrite the following JSX syntax: 使用react 15.1.0react-router 2.5.2 ,我希望重写以下JSX语法:

<Router routes={routes} history={hashHistory}>
  <Redirect from="/" to="/users" />
  <Route path="/" component={Navigation}>
    <Route path="/users" component={UsersPage}/>
    <Route path="/endpoints" component={EndpointsPage}/>
  </Route>
</Router>

Into plain routes, like this: 进入平原路线,像这样:

const routes = {
  path: '/',
  component: Navigation,
  childRoutes: [
    {path: '/users', component: UsersPage},
    {path: '/endpoints', component: EndpointsPage}
  ],
  onEnter: (nextState, replace) => replace('/users')
};

React.render(<Router routes={routes} history={hashHistory}/>, element);

However the result of the latter is always: 然而,后者的结果总是:

Uncaught RangeError: Maximum call stack size exceeded 未捕获RangeError:超出最大调用堆栈大小

The onEnter: (nextState, replace) => replace('/users') is apparently not sufficient to replace the <Redirect from="/" to="/users" /> . onEnter: (nextState, replace) => replace('/users')显然不足以将<Redirect from="/" to="/users" /> How can I rewrite the redirect successfully? 如何成功重写重定向? Is there a better way to make navigation to / always end up at /users ? 有没有更好的方法来导航/总是在/users

There is a thing called IndexRedirect which I was unaware of. 有一个名为IndexRedirect的东西,我不知道。 Using index redirect, the correct way to do this in plain routes is: 使用索引重定向,在普通路由中执行此操作的正确方法是:

const routes = {
  path: '/',
  component: Navigation,
  indexRoute: {onEnter: (nextState, replace) => replace('/users')},
  childRoutes: [
    {path: '/users', component: UsersPage},
    {path: '/endpoints', component: EndpointsPage}
  ]
};

React.render(<Router routes={routes} history={hashHistory}/>, element);

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

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