Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应路由器:返回到特定页面_Reactjs_React Router - Fatal编程技术网

Reactjs 反应路由器:返回到特定页面

Reactjs 反应路由器:返回到特定页面,reactjs,react-router,Reactjs,React Router,我有一个设置页面,其中有几个子路由: --/home --/articles --/settings --/Account(default) --/Help --/About 我希望这样:当我在“设置”下的任何一个子页面中,并且浏览器返回历史记录时,它应该始终转到(默认)帐户页面。再次返回将遵循正常的浏览器历史记录行为 最好的处理方法是什么?您可以使用: 从“历史记录/createBrowserHistory”导入createHistory const history=create

我有一个设置页面,其中有几个子路由:

--/home
--/articles
--/settings
  --/Account(default)
  --/Help
  --/About
我希望这样:当我在“设置”下的任何一个子页面中,并且浏览器返回历史记录时,它应该始终转到(默认)帐户页面。再次返回将遵循正常的浏览器历史记录行为

最好的处理方法是什么?

您可以使用:

从“历史记录/createBrowserHistory”导入createHistory
const history=createHistory()
历史记录推送(“/account”)

经过相当多的研究,似乎没有简单的方法可以做到这一点,github中的讨论也不会不同意。A似乎是共识。这对我来说有点生硬,但任何有效的都是好的

所以,我没有将每个子页面都设置为具有自己的路由(由react router处理),而是让它们由设置组件中的状态控制。并使用以下事件处理:

componentDidMount = () => {
  window.onpopstate= () => {
    if(this.state.currentSubPage !== 'account-settings') {
      this.props.history.push('/settings');
    } else {
      window.onpopstate = null;
    }
  };
}
componentDidMount = () => {
  window.onpopstate= () => {
    if(this.state.currentSubPage !== 'account-settings') {
      this.props.history.push('/settings');
    } else {
      window.onpopstate = null;
    }
  };
}