也许您可以在配置 rootRoute 时利用 onEnter 和 onChange 回调。
在 onEnter 回调中可以记录初始路由路径。在 onChange 回调中,您可以比较当前/下一个路由路径,检查记录的路径历史记录和记录路径。因此,每次路线变化时,您都可以检查和比较路线路径,您可以停止任何循环链接。
关于保存所有组件的状态,如果你使用redux,整个应用程序的状态可以保存在一个对象中,redux store。
如果你想在离开之前保存组件当时的状态,你可以在componentWillUnmount中发送一个save component state动作,在componentWillMount中恢复状态。
这是一个sn-p:
var rootRoute = {
path: '/',
onEnter: enter,
onChange: change,
component: MyApp,
indexRoute: { component: Home },
childRoutes: [
LoginRoute,
...
{path: 'home', component: Home},
{
path: '*',
component: NotFound
}
]
};
function enter (next) {
// pathStore record all navigation history
pathStore.record(next.location.pathname);
}
function change (cur, next, c) {
// when hit cur path links in nav, pathname is same, key is different.
if (cur.location.pathname !== next.location.pathname) {
...
}
}