gpt4 book ai didi

javascript - 更新 redux 状态对象属性

转载 作者:行者123 更新时间:2023-11-30 11:02:10 26 4
gpt4 key购买 nike

如何在 redux 中更新对象的属性并保留对象的其余部分..

const appReducer = (state = initialState, action) => {
switch (action.type) {
case 'LOGIN_SUCCESS':
return Object.assign({}, state, {
user: { loggedIn: true, level: 'default' },
});

case 'UPDATE_PACKAGE': {
// add a new value for the user.level which would be in action.level
return { ...state, level: action.level };
}

default:
return state;
}
};

所以我希望 UPDATE_PACKAGE 能够更改 redux 存储级别属性的内容...但它没有改变...

最佳答案

所以看起来您是在根状态而不是用户对象上设置level

这应该可以修复它:

 case "UPDATE_PACKAGE": {
return { ...state, user: { ...state.user, level: action.level } };
}

关于javascript - 更新 redux 状态对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270598/

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