gpt4 book ai didi

javascript - 从状态中删除键(ReduxReducer)

转载 作者:行者123 更新时间:2023-11-28 14:10:07 25 4
gpt4 key购买 nike

我在 Redux 中有一个状态,当前呈现如下:

点击前:

{0: {
open: false,
negation: false,
close: false
},
1: {
open: false,
negation: false,
close: false,
bool: "and"
}
}

点击后:

{0: {
open: false,
negation: false,
close: false
},
1: {}
}

我想完全删除 key 1(通常是 [action.id])。

目前reducer中的情况有:

case 'HANDLE_INCREASE_CHANGE':
return {
...state,
index: state.index + 1,
[state.index + 1]: {
open:false,
negation: false,
close: false,
bool: 'and'
}
}
case 'HANDLE_DECREASE_CHANGE':
return {
...state,
index: state.index - 1,
[state.index]: {}
}

错误的部分是:

[state.index]: {}

你能帮我一下吗?非常感谢!

最佳答案

您应该只能调用删除状态[action.id]。尽管在 reducer 函数中,您应该首先获取状态的副本,从中删除,然后返回复制的版本。

case: 'HANDLE_DECREASE_CHANGE':
const next = {...state}
delete next[action.id]
return next

关于javascript - 从状态中删除键(ReduxReducer),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59897667/

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