简体   繁体   English

ReactJS/Redux - 根组件页面在任何页面刷新时呈现

[英]ReactJS/Redux - Root component page is rendered on refresh from any page

My app has a default route that loads the component HomePage on the localhost/ path in dev mode.我的应用程序有一个默认路由,它在开发模式下在localhost/路径上加载组件HomePage

But when I refresh the app from any page, the home page is displayed instead, even though the URL in my browser displays localhost/any/other/path .但是,当我从任何页面刷新应用程序时,主页会改为显示,即使浏览器中的 URL 显示localhost/any/other/path If I then navigate manually through the app to the /any/other/path path, the browser's URL becomes localhost/any/other/path/any/other/path .如果我随后通过应用程序手动导航到/any/other/path路径,则浏览器的 URL 将变为localhost/any/other/path/any/other/path

It's like the router always thinks the current path is / on refresh.这就像路由器总是认为当前路径是/刷新。

What could the issue be?可能是什么问题? I'm using React 16 and "react-router-dom": "5.1.2" .我正在使用 React 16 和"react-router-dom": "5.1.2"

My routes are laid out as follows:我的路线安排如下:

<Provider store={store}>
  <ConnectedRouter>
    <div>
      <Switch>
        <Route exact path="/" component={HomePage} />
        <Route exact path="/login" component={LoginPage} />
        <Route exact path="/proxy" component={ProxyPage} />
        ...
      </Switch>
    </div>
  </ConnectedRouter>
</Provider>

And my Redux router reducer is as follows:我的 Redux router减速器如下:

import { createBrowserHistory } from 'history'
const history = createBrowserHistory({
  basename: window.location.pathname
})

// Reducers
combineReducers({
  ...
  router: connectRouter(history),
  ...
})

In the store the router reducer starts as follows, even when loading a URL:在商店中, router减速器启动如下,即使在加载 URL 时也是如此:

router: {
  action: "POP",
  location: {
    hash: "",
    pathname: "/",
    query: { },
    search: "",
    state: undefined
  }
}

Edit: Simplified my use case to be more straightforward, added more information编辑:将我的用例简化为更直接,添加了更多信息

I am not 100% sure, but my hunch is that this behavior is more related to using Redux and ConnectedRouter than your routes, which look fine to me.我不是 100% 肯定,但我的预感是这种行为与使用 Redux 和 ConnectedRouter 的关系比你的路由更相关,这对我来说看起来不错。 2 questions: 2个问题:

  1. Are you using hot reloading?你在使用热重载吗?

  2. If you take this same code and run it in a non-redux app with BrowserRouter, what happens?如果您使用相同的代码并在带有 BrowserRouter 的非 redux 应用程序中运行它,会发生什么?

When true, redirecting will push a new entry onto the history instead of replacing the current one.当为 true 时,重定向会将新条目推送到历史记录中,而不是替换当前条目。

Redirect push to="/somewhere/else" /重定向推送到="/somewhere/else" /

Are you using the Redirect push in your code?您是否在代码中使用重定向推送?

Found the issue.发现问题。

I had changed the default basename argument in my browserHistory's configuration, which did not work as I understood (I was trying to fix a lack of redirection issue on app launch).我在我的 browserHistory 的配置中更改了默认的basename参数,这在我的理解中不起作用(我试图解决应用程序启动时缺少重定向的问题)。

Removing the basename: window.location.pathname line fixed my (self-inflicted) problem.删除basename: window.location.pathname行修复了我的(自己造成的)问题。

<Provider store={store}>
  <ConnectedRouter>
    <div>
      <Switch>
        <Route exact path="/">
          <Redirect to="/home" />
        </Route>
        <Route exact path="/login" component={LoginPage} />
        <Route exact path="/proxy" component={ProxyPage} />
        ...
      </Switch>
    </div>
  </ConnectedRouter>
</Provider>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM