当我查看通过combineReducers
调用的reducer时,它没有在参数中获取状态或操作。
我的Redurs文件是:
import { combineReducers } from 'redux';
import nav from './reducers/nav';
import pages from './reducers/pages';
import entities from './reducers/entities';
export default function(initialData) {
return function(state, action) {
if (!state) {
console.log('no state', initialData);
return initialData;
}
// This is showing state as having expected value
console.log('state', state);
return combineReducers({
nav,
pages,
entities
});
}
};
我的商店初始化是这样的:
import reducerWrapper from './reducers';
// Initial state is defined above
var reducers = reducerWrapper(initialState),
store = Redux.createStore(reducers, initialState);
example code on the site没有使用包装器(我在其他一些例子中看到过)。我也试过了,但它没有用。我的意思是在任何一个例子中我都不确定如果得到的是什么会得到状态/动作。我觉得我在这里错过了一些魔法。
更新 现在是reducers文件:
import { combineReducers } from 'redux';
import nav from './reducers/nav';
import pages from './reducers/pages';
import entities from './reducers/entities';
export default combineReducers({
nav,
pages,
entities
});
后跟store = Redux.createStore(reducerWrapper, initialState)
,即使我删除reducerWrapper
内的包装并只导出默认的combineReducers`
答案 0 :(得分:1)
答案在这里不容易看到。问题是我的reducer没有默认状态。
商店在此处触发INIT事件:https://github.com/reactjs/redux/blob/master/src/createStore.js#L204
然后最终到达这里: https://github.com/reactjs/redux/blob/master/src/combineReducers.js#L52
意思是如果我的reducer是function (state, action) { return state }
而不是function (state='anyDefaultValue') { return state }
,combineReducer
会说错误说减速器没有返回状态
答案 1 :(得分:0)
combineReducers
返回需要使用state
和action
调用的函数。你可以直接导出它:
const rootReducer = combineReducers(...);
export default rootReducer;
或者你可以像现在一样包装它:
const rootReducer = combineReducers(...);
export default function (initialData) {
return function (state, action) {
if (!state) {
console.log('no state', initialData);
return initialData;
}
// This is showing state as having expected value
console.log('state', state);
return rootReducer(state, action);
}
}