React Routing,刷新不渲染具体视图,而是重定向到父视图

React Routing, refresh does not render specific view, but redirects to parent view

这是我们的父视图: http://localhost:8080/#/dashboard/

当我点击 link 进入菜单导航时: http://localhost:8080/#/dashboard/menu-navigation

这将加载正确的 menu-navigation 视图,但是如果我刷新应用程序将返回 /dashboard 而不是停留在 menu-navigation.

从主Routes.js

const Routes = () => (
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <Switch>
        <Route path="/login" exact component={Login} />
        <Route path="/redirect" component={FirebaseRedirector} />
        <Route path="/restricted-access" component={RestrictedAccess} />
        <Route path="/change-password/:hash?" component={ChangePassword} />
        <Route path="/reset-password" exact component={ResetPassword} />
        <Route path="/" component={Main} />
        <Route path="*" component={NotFound} />
      </Switch>
    </ConnectedRouter>
  </Provider>
);

export default Routes;

请注意,一旦您登录,上面的 Main 组件就会加载:

render() {
  return (
    <Main
      user={this.props.user}
      signOut={this.signOut}
    >
      <Switch>
        <Route
          path="/dashboard/categories/unmatched-api/:id?"
          component={CategoriesUnmatchedApi}
          notExact
        />
        <Route path="/dashboard/menu-navigation" exact component={MenuNavigation} />
        <Route path="/dashboard/home-screen" exact component={HomeScreen} />
        <Route path="/dashboard/categories/product-config" component={CategoryProductConfig} />
        <Route path="/dashboard/categories/:category?" component={Categories} />
        <Route path="/dashboard/playground" exact component={Playground} />
        <Route path="/dashboard" exact component={Drafts} />
        <Route path="/" exact component={Drafts} />
        <Route path="*" component={NotFound} />
      </Switch>
    </Main>
  );
}

这是您导航到 MenuNavigation 路线后的状态

然后是刷新后的状态

似乎 React 路由器不遵守 <Route path="/dashboard/menu-navigation" ?

刷新后我确实注意到 Main.js 路线部分被命中 3 次,第一次 this.props.location 是正确的,但是接下来的 2有时只是 /dashboard

想通了!我们每次都会重定向到 /dashboard。

在我们的登录操作中,我们有:

onAuthStateChange -> checkAuth 其中包含:

dispatch(push(authDasboardView));

删除对 authDashboardView 的调度后,这解决了问题!