Javascript Redux将值指定给已包含来自其他减速器的值的对象
所以我得到了这个代码:Javascript Redux将值指定给已包含来自其他减速器的值的对象,javascript,reactjs,redux,Javascript,Reactjs,Redux,所以我得到了这个代码: case LOGIN_SUCCESS: return Object.assign({}, state, { message: "Login successful", cause: null, username: action.username, fullname: action.fullname, credit: action.credit, id: act
case LOGIN_SUCCESS:
return Object.assign({}, state, {
message: "Login successful",
cause: null,
username: action.username,
fullname: action.fullname,
credit: action.credit,
id: action.id,
topmenutype: action.topmenutype,
authenticated: true
});
case LOCATION_SUCCESS:
return Object.assign({}, state, {
location: action.location
});
所以基本上我想做的是把
location
添加到上面的对象中。这可能吗?你可以做类似的事情,就像你在上面做的那样
return Object.assign({}, {
...state,
location: action.location
});
您不需要使用
对象。分配并创建对象的深度副本。相反,您可以使用扩展运算符。您可以阅读更多关于它们的用法和区别
我看不出你的代码有问题。你有错误吗?你所说的“上面的物体”有几个候选词。如果你指的是state对象,那么看起来你已经得到了你想要的东西。如果出现问题,那么我不清楚您的问题。我想做的是向LOGIN_SUCCESS返回的对象添加“location”值。您已经编写了该代码。为什么你认为它不起作用?顺便问一下,你知道Object.assign是如何工作的吗?你也可以在LOGIN\u成功案例下添加location
键,就像你添加了id、credit等一样。它可以正常工作。这就是你想要的吗?这个代码(除了扩展操作符)已经在问题中了。不确定您想建议什么。这是一个关于spread运算符的有用语法提示,但在功能上与已发布的代码等效。你想建议什么?
case LOGIN_SUCCESS:
return {
...state,
message: "Login successful",
cause: null,
username: action.username,
fullname: action.fullname,
credit: action.credit,
id: action.id,
topmenutype: action.topmenutype,
authenticated: true
};
case LOCATION_SUCCESS:
return {
...state,
location: action.location
};