减速器上的功能
从状态中删除所选类别的功能
更新添加状态
state={cat:[]}
const delCat = (state, payload) => {
const { id } = payload
const idx = state.category.findIndex((el) => el.id === id)
let pastState = state.slice(0, idx)
let futureState = state.slice(idx + 1)
let newState = [...pastState, ...futureState]
return {
...state,
category: newState,
}
}
减速器
const initialState = {
category: [
{
id: 1,
name: 'Изброное',
color: '#5236C1',
list: [{}]
сonst newcat = (state = initialState, action) => {
switch (action.type) {
case 'DEL_CATEGORY':
return delCat(state, action.payload)
default:
return state
}
}
动作
export const delCat = (id) => ({
type: 'DEL_CATEGORY',
payload: { id },
})
文件调度
const DelCategory = (id) => {
dispatch(delCat(id))
}
p.s.提前感谢
您在对象上而不是在类别数组上调用slice。
所以改变
let pastState = state.slice(0, idx)
let futureState = state.slice(idx + 1)
至
let pastState = state.category.slice(0, idx)
let futureState = state.category.slice(idx + 1)
考虑到category
是一个数组,您可能还想将名称更改为类似categories
的名称
您的状态是一个Object。所以你不能在上面使用切片法。
我想你是想写这个的;
state.category.slice(0, idx)