Redux 的 Store.dispatch 的问题没有得到更新



升级meteor(从1.4到1.7(和react(从15.3.2到16.8.6(。

"react-redux": "^4.4.10"
"redux": "3.5.2"

我发现我的代码无法使用store.dispatch((更新/存储,store只是没有更新。

我的操作文件如下:

actions/config.js

...
export default {
load({Meteor, Store}) {
return new Promise((resolve, reject) => {
Meteor.call('variables.load', null, (err, data) => {
if (err) {
reject({_error: err.reason });
return;
}
console.log("************ Store (A) = "+JSON.stringify(Store.getState()))
Store.dispatch({
type: LOAD_CONFIG,
data
});
resolve();
console.log("************ Store (B) = "+JSON.stringify(Store.getState()))
});
});
},
...

两个console.log((都有以下内容:

Store (A) = {"router":{"locationBeforeTransitions":{"pathname":"/settings/config","search":"","hash":"","action":"PUSH","key":"zif4ls","basename":"/crm","query":{}}},"form":{"config":{"syncErrors":{"reportLimit":"Required"}}},"loadingBar":{}}
Store (B) = {"router":{"locationBeforeTransitions":{"pathname":"/settings/config","search":"","hash":"","action":"PUSH","key":"zif4ls","basename":"/crm","query":{}}},"form":{"config":{"syncErrors":{"reportLimit":"Required"}}},"loadingBar":{}}

我确实预计它会有类似"reportLimit":6的东西,它已被确认加载到data变量中。相反,我在浏览器控制台中得到了以下错误:

Uncaught TypeError: Cannot read property 'data' of undefined

你能想到什么错误/突破性的改变吗?因为这些代码在升级之前就已经工作了。


编辑:

我进一步缩小了问题的范围。可能是我的Routes没有叫减速器。

从那以后,我更改了Routes中的代码,以消除使用require.securve.的需要

路由.jsx(以前(

{
path: 'config',
getComponent(nextState, cb) {
require.ensure([], (require) => {
Store.injectReducer('config', require('./reducers').config)
cb(null, require('./containers/config.js'))
}, 'config')
}
},

路由.jsx(最新,摆脱require.ensure(

{
path: 'config',
getComponent(nextState, cb) {
import('./containers/config.js')
.then(mod => {Store.injectReducer('config', require('./reducers').config); 
cb(null, mod);});
}
},

然后我注意到在减速器中:

reductor/config.js

// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
[LOAD_CONFIG]: (state, action) => ({
...state,
data: action.data
})
};
// ------------------------------------
// Reducer
// ------------------------------------
const initialState = {
data: null
};
export default function configReducer(state = initialState, action) {
console.log("************ Reducer")
const handler = ACTION_HANDLERS[action.type];
return handler ? handler(state, action) : state;
}

根据记录,函数configReducer似乎没有在其他地方被调用。

经过多次尝试和错误,确认问题出在路由部分,最终更改:

路由.jsx(最终(

{
path: 'config',
getComponent(nextState, cb) {
import('./containers/config').then(mod => {
Store.injectReducer('config', require('./reducers/config').default);
cb(null, mod.default);
});
}
}

要点:1( 这就是如何在不依赖webpack的情况下从require.ensure(由webpack使用(迁移到(我完全使用Meteor Atmosphere运行时就是这样(2( 如果reducer函数导出为export default,则modrequire(...).xxx已更改为mod.defaultrequire(...).default,否则将不会调用所述reducer

我真的花了好几个星期才弄明白!

试试这个:

Store.dispatch({
type: LOAD_CONFIG,
data: data
});

最新更新