首页 > 解决方案 > 有没有办法让我可以访问浏览器历史记录中的所有项目或至少历史的顶部项目

问题描述

我有一个场景,我必须在提交表单时采取一些行动。

我从两条途径得出这种特殊形式。比方说 /first-route 和 /second-route

现在我的目标很简单,如果我来自 /first-route,我必须在提交表单时返回 /first-route。但如果我来自 /second-route 或其他任何地方,我必须去 /third-route。

有没有办法访问 react-router 历史对象中的最后一项,以便我可以有条件地进行此路由。

该功能history.goBack()将我带回最后一条路线,但我想要的是在返回之前先检查这条路线

如果路线是 /first-route 我想做的history.push('/first-route')。在其他情况下我必须这样做history.push('/third-route')

此外,对于更多上下文,我有两个独立的反应应用程序,必须在它们之间进行这种通信。

我有两个应用程序嵌入在第三个应用程序中。我想让父应用程序独立于这种通信。

标签: reactjsreact-routerreact-router-v4browser-historyreact-router-dom

解决方案


属性 每个历史对象都具有以下属性:

history.length - The number of entries in the history stack history.location - The current location (see below) history.action - The current navigation action (see below)

此外,createMemoryHistory 提供了 history.index 和 history.entries 属性,可让您检查历史堆栈。

https://www.npmjs.com/package/history


推荐阅读