从状态数组中删除项目 - redux

时间:2017-12-27 00:57:59

标签: arrays react-native redux react-redux

我正在使用redux处理react-native app。我无法从数组中删除特定项目。 state.tournaments是数组,我想要删除的项目必须包含我从动作发送到redux的ID。

这是我的减速机:

import {
  TOURNAMENT_NAME_CHANGED,
  TOURNAMENT_CREATE,
  SAVE_TOURNAMENT,
  DELETE_TOURNAMENT
} from '../actions/types';

const INITIAL_STATE = {
  name: null,
  admin_token: null,
  tournaments: []
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case TOURNAMENT_NAME_CHANGED:
        return { ...state, name: action.payload };
    case TOURNAMENT_CREATE:
        return { ...state, admin_token: action.payload.data.admin_token };
    case SAVE_TOURNAMENT:
        return { ...state, tournaments: [...state.tournaments, action.payload] };
    case DELETE_TOURNAMENT:
        return { ...state, tournaments: state.tournaments.filter((name, id) => id !== action.payload.id) };
    default:
      return state;
  }
};

1 个答案:

答案 0 :(得分:1)

您没有正确使用过滤器,请尝试以下操作:

state.tournaments.filter(tournament => tournament.id !== action.payload.id)