[英]React : update one value of my state in reducer
I try to modify a value of my current state with redux. 我尝试使用redux修改当前状态的值。 Here my state :
这是我的状态:
tickets : {
high_tickets: [
{
"value": value,
"value": value,
"value": value,
"value": value,
"visibility": 1
},
{
...
}
],
low_tickets: [
...
]
}
Here my ticket reducer : 这是我的机票折扣器:
case 'HIDE_TICKET':
const i = action.index
if(action.typeFraud == "high") {
state = {
"high_tickets": [
...state.high_tickets.slice(0,i),
{
...state.high_tickets[i],
"visibility": 0
}
...state.high_tickets.slice(i + 1),
],
"low_tickets": [
...state.low_tickets
]
}
}
else if(action.typeFraud == "low") {
...
}
return state
break;
I have an error at the line {...state.high_tickets[i],"visibility": 0}
. 我在
{...state.high_tickets[i],"visibility": 0}
行上出错。 Unexpected tocken. 意外的炸鸡。
How can I modify only one value of my object and not redifine all values in my reducer. 如何仅修改对象的一个值,而不重新定义化简器中的所有值。
Thanks. 谢谢。
You can use Object.assign(state.high_tickets[i], { visibility: 0 })
if you want to mutate the existing object, or Object.assign({}, state.high_tickets[i], { visibility: 0 })
if you want to create an entirely new object. 如果要更改现有对象,可以使用
Object.assign(state.high_tickets[i], { visibility: 0 })
也可以使用Object.assign(state.high_tickets[i], { visibility: 0 })
Object.assign({}, state.high_tickets[i], { visibility: 0 })
如果要创建一个全新的对象。
It will look like: 它看起来像:
"high_tickets": [
...state.high_tickets.slice(0,i),
Object.assign(...),
...state.high_tickets.slice(i + 1),
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.