javascript - React Redux 无法读取未定义的属性 'dispatch'

标签 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"

但我得到了错误

Cannot read property 'dispatch' of undefined



这是我的 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
添加/删除软件包,我知道导致该问题的软件包是“react-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);

最佳答案

更改自:

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')
);

关于javascript - React Redux 无法读取未定义的属性 'dispatch',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54862124/

相关文章:

reactjs - 更新存储在 jwt 负载中的字段

javascript - Redux 和 Angular 2

javascript - 如何在像 facebook 一样加载时创建占位符

javascript - angular2 在具有自定义可序列化对象的后台线程中工作

javascript - 使用 momentjs,我如何判断 2 个时刻是否代表同一天(不一定是同一时间)?

javascript - 如何在 React JS 中设置环境变量..?

reactjs - 工具栏draftjs编辑器中的自定义按钮

javascript - 在谷歌地图 Angular 查找距离

javascript - 为什么我的 User.props 总是未定义? React-Native 和 Redux

javascript - 将正则表达式保存到 Redux 存储中?