Davor Buha
Davor Buha

Reputation: 31

Delete items from state array - redux

I'm working on react-native app with redux. I can't delete specific item from array. state.tournaments is array and item which i want to delete must contain ID which I'm sending from actions to redux.

This is my reducer:

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;
  }
};

Upvotes: 0

Views: 933

Answers (1)

Matt Aft
Matt Aft

Reputation: 8936

You're not using filter correctly, try this:

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

Upvotes: 1

Related Questions