4

我一直在尝试设置react++ ,react-router但没有成功。一切似乎都很好,直到我添加到混合物中,之后我得到reduxredux-simple-routerredux-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;

4

1 回答 1

3

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

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

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

像这样升级我的包:

 history       ^1.17.0  →  ^2.0.0-rc2
 react-router   ^1.0.3  →  ^2.0.0-rc5

...解决了错误。

于 2016-01-22T15:48:35.817 回答