我有以下服务器代码:
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
正在此处调用该方法。
但是浏览器不会重定向。
什么会导致浏览器不重定向?
事实证明我有 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}>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句