javascript - React Routing,刷新不渲染特定 View ,而是重定向到父 View

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

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

当我单击菜单导航链接时: http://localhost:8080/#/dashboard/menu-navigation

这将加载正确的 menu-navigation View ,但是如果我刷新应用程序将返回到 /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 路线后的状态

enter image description here

然后是刷新后的状态

enter image description here

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

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

enter image description here

最佳答案

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

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

onAuthStateChange -> checkAuth 其中包含以下内容:

调度(push(authDasboardView));

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

关于javascript - React Routing,刷新不渲染特定 View ,而是重定向到父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227424/

相关文章:

javascript - Redux Thunk 中间件不工作

reactjs - React 惰性组件未加载到动态路由上

javascript - 谷歌地图标签放置

javascript - 在热敏打印机上进入 ESC/POS 模式

javascript - Next.js 在嵌套子组件中获取数据

javascript - React Router 中的多个可选参数

reactjs - 嵌套路由未显示正确的组件

javascript - 如何使用 Canvas JS 制作多个圆圈

javascript - Discordjs 在前缀和命令之间添加空格

javascript - 如何阻止背景样式覆盖 React 内联样式的 backgroundColor 样式?