0

so I have a reducer that is adding to array create reducer :

export default (itemsList = [], action) => {
    if (action.type === 'ADD_ITEM') {
        return [...itemsList, action.payload]
    }
    return itemList
}

deleting reducer (99% that something is wrong here, but I have no idea what ):

export default (itemList = [], action) => {
    if (action.type === 'DELETE_ITEM') {
        return [...itemList, itemList.filter(item => item !== action.payload)]
    }
    return itemList
};

action/index.js:

export const addItemToList = item => {
    return {
        type: 'ADD_ITEM',
        payload: selectedItem
    }
};

export const deleteItemFromList = item => {
    return{
        type: 'DELETE_ITEM',
        payload: selectedItem
    }
};

let say I have itemList = [ 'abc', 'xyz', 'qwe' ]

and I want to use deleteItem('xyz') to delete 'xyz' from itemList

4
  • What happens when you deleteItem('xyz')? Commented May 4, 2019 at 17:50
  • problem is that still nothing is happening. it doesnt delete the value from array and still i dont know what to do ... Commented May 4, 2019 at 20:20
  • what are the elements of the array? is it a primitive or an object? if an object can you write out its structure and what is the value of the payload, meaning what property are you targeting? Commented May 4, 2019 at 20:38
  • I have a dropdown that user can select multiple options and the selected values are in array as a strings. Then im showing user the values that he picked with button that schould delete them when user click on it. Buton is working fine. I bind key to the button and when im using console.log(key) it shows right key, but when I want to delete it, nothing is happening. Commented May 4, 2019 at 21:05

2 Answers 2

1

While deleting you just need to return the filtered list and not use spread operator too.

export default (itemList = [], action) => {
    if (action.type === 'DELETE_AUTHOR') {
        return itemList.filter(item => item !== action.payload)
    }
    return listOfAuthorsSelected
};
Sign up to request clarification or add additional context in comments.

1 Comment

Sadly im trying all the time and cant make it work at all
0

Array.filter() returns a new array with given filter condition and not mutate the existing array. You don't have need to use ...itemList(spread operator). Here you are actually adding a sub array each time.

Here is a simple running example

var array1 = ["abc", "def" , "ghi"];

var array2 = [...array1, array1.filter((item) => {
return item !== "def"
})];

document.write(array2);


// correct way to filter
var array3 = ["abc", "def" , "ghi"];

var array4 = array3.filter((item) => {
return item !== "def"
});

document.write("<hr/>"+array4);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.