1
当我直接使用Redux的减速机,我得到预期的状态:使用终极版combineReducers是创建一个嵌套的状态
import cheese from 'reducers/cheese.js';
const store = createStore(cheese);
<Provider store={ store } >
...
</Provider>
结果:
cheese: "cheddar"
但是当我使用combineReducer
我得到一个嵌套状态
import cheese from 'reducers/cheese.js';
import crackers from 'reducers/crackers.js';
const reducer = combineReducers({ cheese, crackers });
const store = createStore(reducer);
<Provider store={ store } >
...
</Provider>
结果:
cheese: { cheese: "cheddar" }
这导致浅状态比较验证,并且我的组件不更新。 (编辑:这种假设可能是假的)
编辑:
我的理想状态是这样的(这是个什么样子使用单个减速时等):
{
cheese: "cheddar",
crackers: "ritz"
}
编辑:
我的减速器是这样的(两者相同):
const initialState = {
cheese: 'cheddar'
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case 'newCheese':
console.log(action.newCheese); // "cheddar"
return {
...state,
cheese: action.newCheese
};
default:
return state;
}
}
这是combineReducers'的'的目标,所以我不知道你的问题是什么,因为它的表现如预期。你的'奶酪'减速器似乎会返回一个对象,而不是一个值。 – RIAstar
你想如何结合奶酪和薄脆饼干来照顾国家?你可以编辑你的问题,并提供您的预期输出? –
我编辑了这个问题 - 谢谢! – Sauce