I'm trying to add a single item to my nested Redux state, if that item isn't already in the list.
I don't care about doing it in plain JS, I want the Redux sauce ofc.
My state looks like this:
state: {
data: {
list: []
},
},
This is what I tried in my reducer:
return {
...state,
data: {
...state.data,
list: [action.payload.list, ...state.data.list]
}
}
Problem is, sometimes the item I want to add is already in the array, therefore I have a duplicate in my list (which I don't want).
How can I add an item to my list while checking first if this item is already there?