我对我的应用程序包进行了一些更新,例如:
"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):
有什么建议么?
更新 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/