[英]Update redux state object property
How do I update the property of an object in redux and preserve the rest of the object.. 如何在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;
}
};
So I would expect the UPDATE_PACKAGE to change the contents of the redux store level property... but its unchanged... 所以我希望UPDATE_PACKAGE能够改变redux商店级别属性的内容......但是它没有改变......
So it seems like you're setting level
on the root state instead of the user object. 因此,您似乎在根状态而不是用户对象上设置level
。
This should fix it: 这应该解决它:
case "UPDATE_PACKAGE": {
return { ...state, user: { ...state.user, level: action.level } };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.