React Routing,刷新不呈现特定视图,但重定向到父视图

时间:2017-11-10 16:40:16

标签: javascript reactjs react-router react-router-redux

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

当我点击菜单导航的链接时: 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;

注意一旦您登录,上面的主要组件已加载:

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路线

,这是州

enter image description here

然后刷新后的状态

enter image description here

似乎反应路由器不尊重<Route path="/dashboard/menu-navigation"

我在刷新后注意到 Main.js 路由部分被击中3次,这是第一次this.props.location正确,但接下来的2次只是{{1} }

enter image description here

1 个答案:

答案 0 :(得分:0)

想出来!我们每次都有一个重定向到/仪表板。

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

onAuthStateChange - &gt; checkAuth包含此内容:

dispatch(push(authDasboardView));

将调度删除到authDashboardView后,这解决了问题!