【问题标题】:How React router hashHistory goBack function worksReact 路由器 hashHistory goBack 函数是如何工作的
【发布时间】:2018-12-06 05:55:07
【问题描述】:

我正在使用React-router 3.2.0,所以我在应用程序中有一个按钮,我在该按钮上调用了 hashHistory 的 goBack 函数,如下所示

Router.hashHistory.goBack();

我工作正常,但我需要在调用之前添加一些条件。所以我的问题就像我在 3 个链接之间移动

  1. 首页
  2. 仪表板
  3. 设置

最初它会在Home,然后我搬到Dashboard,然后是Settings。现在我点击了我调用goBack()函数的按钮,当时当前的历史位置是Settings它移动到Dashboard然后Home到达Home后没有这样的历史位置存在。

所以这是我需要检查是否仍然存在任何历史记录位置。像

if(/*history exist*/){
 call goBack();
} else {
 //do my stuff
}

在 react-router 3.2.0 版本中有什么办法吗?

【问题讨论】:

标签: javascript reactjs react-router history browser-history


【解决方案1】:

react-router 中的history 对象有一个length property indicating how many entries are in the stack

if (history.length !== 0) {
 goBack();
} else {
 // do my stuff
}

【讨论】:

  • 你说的是 window.history 还是 react-router 中可用的任何 History 对象?
  • @KushalJain react-router 中可用的历史对象。
  • 我试过像 import {History} from 'react-router';然后 console.log(History) 它发生未定义。
  • @KushalJain history 对象不是直接从react-router 导出的,但是你可以得到它like described in this answer
猜你喜欢
  • 2017-02-26
  • 2019-06-17
  • 2016-07-17
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 2017-06-04
相关资源
最近更新 更多