TypeError:state.slice 不是一个函数(redux)
2021-03-31
833
Reducer 上的函数
从状态中删除所选类别的函数
更新添加的状态
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,
}
}
Reducer
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
}
}
Action
export const delCat = (id) => ({
type: 'DEL_CATEGORY',
payload: { id },
})
文件调度
const DelCategory = (id) => {
dispatch(delCat(id))
}
附言:提前致谢
2个回答
您正在对对象而不是类别数组调用切片。
因此将
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
Jap Mul
2021-03-31
您的状态是一个对象。因此您不能对其使用切片方法。
我认为您是想这样写;
state.category.slice(0, idx)
İlker
2021-03-31