Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 数据更新中未定义减速器返回_Reactjs_React Native_Redux_React Redux_Reducers - Fatal编程技术网

Reactjs 数据更新中未定义减速器返回

Reactjs 数据更新中未定义减速器返回,reactjs,react-native,redux,react-redux,reducers,Reactjs,React Native,Redux,React Redux,Reducers,我正在尝试创建一个减速机,以便在调用某个操作后删除对象,但我无法在数据中更新和保存新对象 减速器: export default function deleteCard(state = INITIAL_STATE, action: any) { if (action.type === 'DELETE_CARD') { return state.data.filter((card: Card) => card.cardId !== action.id) } else {

我正在尝试创建一个减速机,以便在调用某个操作后删除对象,但我无法在数据中更新和保存新对象

减速器:

export default function deleteCard(state = INITIAL_STATE, action: any) {
  if (action.type === 'DELETE_CARD') {
    return state.data.filter((card: Card) => card.cardId !== action.id)
  } else {
    return state
  }
}
TypeError: undefined is not an object (evaluating 'state.data.filter')  
行动删除卡

export const deleteCardAction = (id: number) => {
  return {
    type: 'DELETE_CARD',
    id: id
  }
}

函数handleDeleteCard()用于更新状态,您需要从reducer返回整个状态,如下所示:

export default function deleteCard(state = INITIAL_STATE, action: any) {
  if (action.type === 'DELETE_CARD') {
    return {
        ...state,
        data: state.data.filter((card: Card) => card.cardId !== action.id)
    }
  } else {
    return state
  }
}

您如何使用
deleteCard
reducer?它似乎没有接收到状态,但我们无法知道为什么,除非看到您如何使用它。我已经更新了执行reducer的部分代码。您需要在每个reducer案例中返回整个状态。您只是返回筛选后的state.data数组,这样现在就成为状态。@谢谢,下面给出的答案应该可以解决这个问题,这绝对是个好建议。
TypeError: undefined is not an object (evaluating 'state.data.filter')  
export default function deleteCard(state = INITIAL_STATE, action: any) {
  if (action.type === 'DELETE_CARD') {
    return {
        ...state,
        data: state.data.filter((card: Card) => card.cardId !== action.id)
    }
  } else {
    return state
  }
}