简体   繁体   English

Isomorphic react-router-redux syncHistory中间件

[英]Isomorphic react-router-redux syncHistory middleware

syncHistory from react-router-redux takes in an argument that is browserHistory from react-router . syncHistoryreact-router-redux取入是一个参数browserHistoryreact-router On the server, calling syncHistory(browserHistory) does not really work. 在服务器上,调用syncHistory(browserHistory)并不真正起作用。 I am trying to create a store with middleware: 我正在尝试使用中间件创建一个商店:

const { syncHistory } from 'react-router-redux'
const { browserHistory } from 'react-router'
const { createStore, applyMiddleware } from 'redux'

const reduxRouterMiddleware = syncHistory(browserHistory)
const createStoreWithMiddleware = applyMiddleware(
  routerReduxMiddleware
)(createStore)

On the server, I get the following error: 在服务器上,我收到以下错误:

    unsubscribeHistory = history.listen(function (location) {
                                ^

TypeError: Cannot read property 'listen' of undefined

As you have observed browserHistory will not work on the server as it uses the History API that is built into the browser. 正如您所看到的, browserHistory将无法在服务器上运行,因为它使用浏览器中内置的History API。 On the server you have to substitute it for something like history/lib/createMemoryHistory , eg 在服务器上,你必须用它替换像history/lib/createMemoryHistory这样的东西,例如

import createHistory from 'history/lib/createMemoryHistory';

const reduxRouterMiddleware = syncHistory(createHistory);
...

See the React Router documentation for more information on history. 有关历史记录的更多信息,请参阅React Router 文档


As of React Router 2, docs , createMemoryHistory is included as a part of the React Router. 从React Router 2开始, docs ,createMemoryHistory作为React Router的一部分包含在内。

import createHistory from 'react-router/lib/createMemoryHistory';

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

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