【问题标题】:How to redirect in React Router without changing the url如何在 React Router 中重定向而不更改 url
【发布时间】:2017-07-30 20:51:42
【问题描述】:

我如何实现maybeGoToBar 以便 - 根据名称 - 我显示Foo 组件或Bar 组件而不更改网址?

function maybeGoToBar(nextState, replace, callback) {
  const name = nextState.params.name;
  if (name === "Bob") {
    // do something to route to /bar
    // but keep showing /bob/foo as the current location
    // in the browser address bar
  }
  callback();
}

<Route path=":name/foo" onEnter={maybeGoToBar} component={Foo} >
<Route path="bar" component={Bar} >

【问题讨论】:

  • 听起来您不应该使用路由器来执行此操作。您应该在组件中存储一个状态变量(如果需要,它的初始值可以从 url 中获取)并且您应该根据状态选择要显示的子项。

标签: redirect react-router


【解决方案1】:

您需要使用内存历史记录。

Link

【讨论】:

  • 我读到这个选项的缺点是路由器不处理初始url(用于打开页面)。
猜你喜欢
  • 2019-12-29
  • 1970-01-01
  • 2018-12-14
  • 2011-01-02
  • 2015-04-02
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多