React + Redux Simple Router:找不到undefined的监听

时间:2016-01-22 04:54:02

标签: javascript reactjs redux react-router-redux

我一直试图设置react + react-router + redux + redux-simple-router但没有成功。在我将redux-simple-router添加到混合中之前,一切似乎都能正常工作,之后我会

"[TypeError: Cannot read property 'listen' of undefined]即使我在BrowserHistory对象中传递。

这是代码 -

import React from 'react'
import ReactDOM from 'react-dom'
import { Route, Router, BrowserHistory } from 'react-router';
import HashHistory from 'react-router/lib/HashHistory';
import { compose, combineReducers, applyMiddleware, createStore } from 'redux'
import { Provider } from 'react-redux'
import { syncHistory, routeReducer } from 'redux-simple-router'
import * as reducers from 'reducers/reducers'

import Blank from 'routes/blank';

export default (withHistory, onUpdate) => {


const reducer = combineReducers(Object.assign({}, reducers, {
  routing: routeReducer
}));

const reduxRouterMiddleware = syncHistory(BrowserHistory)
const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore)

const store = createStoreWithMiddleware(reducer)

  return (
    <Provider store={store}>
      <Router history={BrowserHistory} onUpdate={onUpdate}>
        <Route path='/' component={Blank} />
      </Router>
    </Provider>
  );
};

注意:我刚刚发现我正在使用renderToString()在快速服务器中编译反应模板。这是为什么它没有定义?我该怎么办?

注2:如果我改为import BrowserHistory from 'react-router/lib/BrowserHistory',那么我

TypeError: history.listen is not a function;

1 个答案:

答案 0 :(得分:3)

我遇到了同样的错误。我按照这里的教程得到了它:https://github.com/rackt/redux-simple-router/blob/e1df260888d1032d1e68c7694a06b457a4f0130f/README.md

然后我意识到上面有这样的信息:

  

注意:此示例使用当前的react-router的2.0 API   在2.0.0-rc5版本下发布。

Upgrading我的包裹是这样的:

async.apply()

......解决了错误。