为什么 combineReducers 不在 Redux 中接收状态?
Why isn't combineReducers receiving state in Redux?
当我查看通过 combineReducers
调用的 reducer 时,它没有获取参数中的状态或操作。
我的减速器文件是这样的:
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 不使用包装器(我在其他示例中看到过)。我也试过了,但没有用。我的意思是在这两个例子中,我不确定它是如何得到 state/action 给定的。我觉得我在这里缺少一些魔法。
已更新
现在是减速器文件:
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`
combineReducers
returns 需要用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);
}
}
这里不容易看到答案。问题是我的减速器没有默认状态。
商店在此处触发 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
会报错说reducer没有return状态
当我查看通过 combineReducers
调用的 reducer 时,它没有获取参数中的状态或操作。
我的减速器文件是这样的:
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 不使用包装器(我在其他示例中看到过)。我也试过了,但没有用。我的意思是在这两个例子中,我不确定它是如何得到 state/action 给定的。我觉得我在这里缺少一些魔法。
已更新 现在是减速器文件:
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`
combineReducers
returns 需要用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);
}
}
这里不容易看到答案。问题是我的减速器没有默认状态。
商店在此处触发 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
会报错说reducer没有return状态