Reactjs 不能使用3个点来保留对象值,而只更改我想要的值
我在json中有这样一个对象:Reactjs 不能使用3个点来保留对象值,而只更改我想要的值,reactjs,ecmascript-6,redux,react-redux,Reactjs,Ecmascript 6,Redux,React Redux,我在json中有这样一个对象: let state = { authenticationObj: { "isAuthenticating": true, "isAuthentic": false, "subscriberId": "NA", "username": "NA", "firstName": "NA", "lastName": "NA", "enabled": "1", "email": "NA", isAuthorized: false }, lastPass
let state = {
authenticationObj: {
"isAuthenticating": true, "isAuthentic": false, "subscriberId": "NA", "username": "NA",
"firstName": "NA", "lastName": "NA", "enabled": "1", "email": "NA", isAuthorized: false
}, lastPasswordCheckStatus:undefined }
现在我只想更改authenticationObj中的一个项目,比如电子邮件。我知道我应该用。。。因此,如果我要更改lastPasswordCheckStatus,我会这样做:
state = {
...state,
lastPasswordCheckStatus: action.payload.lastPasswordCheckStatus
}
到目前为止还不错。但是如何在authenticationObj中更改电子邮件并保留所有其他属性值,如lastPasswordCheckStatus和firstname等?在使用这种扩展语法时,可以传递多个属性。你只需要在前进的过程中不断地分解结构
let state = {
authenticationObj: {
"isAuthenticating": true,
"isAuthentic": false,
"subscriberId": "NA",
"username": "NA",
"firstName": "NA",
"lastName": "NA",
"enabled": "1",
"email": "NA",
isAuthorized: false
},
lastPasswordCheckStatus: undefined
};
state = {
...state,
lastPasswordCheckStatus: action.payload.lastPasswordCheckStatus,
authenticationObj: {...state.authenticationObj, email: "newEmailValue"}
}
在某一点上,它将开始变得不可读,你可以把它分解成这样的东西
const newAuthenticationObj = {
...state.authenticationObj,
email: "newEmailValue"
}
state = {
...state,
lastPasswordCheckStatus: action.payload.lastPasswordCheckStatus,
authenticationObj: newAuthenticationObj
}
请查看state={……state,authenticationObj:{……authenticationObj,“email:[您的新电子邮件]}}上的分解文档。希望它可以复制