【问题标题】:history.push not causing the location to change when ssrhistory.push 在 ssr 时不会导致位置改变
【发布时间】:2018-12-03 11:36:31
【问题描述】:

我有以下服务器代码:

  const store = configureStore({}, history);

  const context: any = { store };

  const app = (
    <Provider store={store}>
      <StaticRouter location={req.url} context={context}>
        <Application />
      </StaticRouter>
    </Provider>
  );
// render app

以及以下客户端连接路由器:

    <Provider store={store}>
      <ConnectedRouter history={history}>
        <Application />
      </ConnectedRouter>
    </Provider>

我可以看到中间件已正确连接,并且正在调用 history.push 方法 here

但是浏览器不重定向。

什么会导致浏览器不重定向?

【问题讨论】:

  • 那里的history 是指window.history,还是在范围内是否有其他具有该名称的变量?另外,您的 configureStore 方法中有什么?

标签: react-router react-router-redux


【解决方案1】:

原来我有 2 个不同的历史实例,1 个在路由器中间件中,1 个在 ConnectedRouter

确保他们都有相同的实例来解决问题。

我有这个文件,可以在需要的地方导入它:

import { createBrowserHistory } from 'history';
import { createMemoryHistory } from 'history';
import { RouterHistory } from '../types';
const selectedHistory: RouterHistory = typeof window !== 'undefined' ? createBrowserHistory : createMemoryHistory;

export const history = selectedHistory();

然后确保我导入此文件并且不要复制历史记录的多个副本:

import { history } from '../../routes/history';
//etc.
          <ConnectedRouter history={history}>

【讨论】:

  • 你有没有在ssr中使用过可选参数,如果是,我们如何使用? (静态路由器)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2013-05-21
相关资源
最近更新 更多