gpt4 book ai didi

redux - 使用 redux combineReducers 正在创建一个嵌套状态

转载 作者:行者123 更新时间:2023-12-05 07:47:25 25 4
gpt4 key购买 nike

当我直接使用 redux reducer 时,我得到了预期的状态:

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" }

这导致浅状态比较验证,我的组件不更新。 (编辑:这个假设可能是错误的)

编辑:

我想要的状态是这样的(这是使用单个 reducer 时的样子):

{
cheese: "cheddar",
crackers: "ritz"
}

编辑:

我的 reducer 看起来像这样(两者都一样):

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 接受一个对象 - 尝试:

const reducer = combineReducers({ cheese, crackers });

关于redux - 使用 redux combineReducers 正在创建一个嵌套状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com