React Redux无法读取未定义的属性“ dispatch”

时间:2019-02-25 08:25:44

标签: javascript reactjs redux react-redux redux-saga

我对应用程序的软件包做了一些更新,例如:

"react-redux": "^5.0.6" => "^6.0.1",
"redux": "^3.7.2" => "^4.0.1",
"redux-saga": "^0.16.0" => "^1.0.1"

但出现错误“无法读取未定义的属性'dispatch'”

这是我的index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import createSagaMiddleware from 'redux-saga';
import { Provider } from 'react-redux';
import { Route } from 'react-router-dom';
import { ConnectedRouter, routerMiddleware } from 'react-router-redux';
import createHistory from 'history/createHashHistory';
import { writeSaga } from './sagas/writeSaga';
import { readSaga } from './sagas/readSaga';

import App from './App';
import reducers from './reducers';

const history = createHistory();
const sagaMiddleware = createSagaMiddleware();
const middlewares = [routerMiddleware(history), thunk, sagaMiddleware];
const store = createStore(reducers, applyMiddleware(...middlewares));

export default function*  rootSaga() {
  yield [
    writeSaga(),
    readSaga(),
  ]
};
sagaMiddleware.run(rootSaga);

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <Route path="/" component={App} />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('app-container')
);

在这里我得到了错误(react-router-redux ConnectedRouter.js): debugger

有什么建议吗? 谢谢, 安德里亚

更新1 添加/删除软件包后,我知道导致该问题的软件包是“ rect-redux”(5.0.6 => 6.0.1)的升级

更新2 看着react-redux的重大变化,我能够理解问题出在我如何通过商店(breaking changes)。 我将代码更改为:

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history} store={store}>
      <Route path="/" component={App} store={store} />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('app-container')
);

它有效! 但是我知道这不是正确的方法……对于正确的解决方案可能会很有用。

为什么它不能与其他question重复:我要索引的配置相同,我检查了一下,并且确定使用的是react-router- redux。 在我导出的每个页面中,

export default connect(mapDispatchToProps)(Home);

1 个答案:

答案 0 :(得分:0)

更改自:

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <Route path="/" component={App} />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('app-container')
);

致:

ReactDOM.render(
  <Provider>
    <ConnectedRouter history={history} store={store}>
      <Route path="/" component={App} />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('app-container')
);