从状态中删除键(ReduxReducer)

Sku*_*Pak 4 javascript redux

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

点击之前:

{0: {
    open: false,
    negation: false,
    close: false
    },
1: {
    open: false,
    negation: false,
    close: false,
    bool: "and"
    }
}
Run Code Online (Sandbox Code Playgroud)

点击后:

{0: {
    open: false,
    negation: false,
    close: false
    },
1: {}
}
Run Code Online (Sandbox Code Playgroud)

我想完全删除密钥 1 (一般来说它是 [action.id])。

目前减速器中的情况有:

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]: {}
  }
Run Code Online (Sandbox Code Playgroud)

错误的部分是:

[state.index]: {}
Run Code Online (Sandbox Code Playgroud)

你能帮我吗?多谢!

Mat*_*den 8

您应该可以打电话delete state[action.id]。尽管在减速器函数中,您应该首先获取状态的副本,从中删除,然后返回复制的版本。

case: 'HANDLE_DECREASE_CHANGE':
  const next = {...state}
  delete next[action.id]
  return next
Run Code Online (Sandbox Code Playgroud)