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;
}
};
}