react-router-redux重定向到绝对URL

西里尔·查彭

我正在迁移一个React应用程序,并且试图对其进行拆分。基本上,我想将一些客户端响应路由重定向到绝对网址(或相对的,但至少与服务器往返进行反向代理)

注意

里面的所有东西现在都在反应。

路由如下所示:

<Provider store={store}>
  <Router history={history}>
    <Route path="/" component={Root}>
      <IndexRoute component={Home} />
      <Route path="/someroute1" component={Route1} />
      <Route path="/someroute2" component={Route2} />
      <Route path="/someroute3" component={Route3} />
    </Route>
  </Router>
</Provider>

例如,目标是将路由“ /”和“ / someroute2”重定向到静态网址(服务器网址)。因此:

问题很简单:是否可以用绝对的URL替换React Router路由?

我听说过Redirect和IndexRedirect组件,但是我无法弄清楚如何正确使用它,并且由于缺少react / react-router,我无法弄清楚是否会有任何危险的副作用(历史上例如)。

西里尔·查彭

部分基于@brub答案,我发现了使用哑组件的解决方案。

import React, { Component } from 'react'

export default class MyRedirectRoute extends Component {
  componentDidMount() {
    window.location.href = //my url here
  }
  render() {
    return null
  }
}

我这样通过

<Route path="/someroute3" component={MyRedirectRoute} />

虽然,我仍然不知道一些事情:

  • 这是推荐的解决方案吗?
  • 有历史副作用吗?
  • 有没有比这更好的(更多反应路由器)解决方案window.location.href我尝试this.context.history没有成功...

在接受自己的答案之前,我正在等待反馈/更好的解决方案

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章