well, I have this reducer which has 2 actions: add an item and remove the item. add item work fine but the problem is with remove item, when I use filter method it will remove all items with the same name(duplicate), but I want to remove only the clicked element.
I tried to use splice method by passing the item index as payload instead of a name, creating new array same as basket items and applying splice method to this array but the problem is when I assign the new array to basket items it changes but doesn't re-render the items!
hope anyone can help thank u!
import {ADD_ITEM,REMOVE_ITEM} from "./Actions-Type"
var initialState ={
baskeitems:[" Strawberry"," Blueberry","Blueberry","Blueberry","Strawberry"]
};
function Rootreducer (state=initialState,action){
if (action.type === ADD_ITEM) {
return Object.assign({}, state, {
baskeitems: state.baskeitems.concat(action.payload)
})
}
else{
if(action.type===REMOVE_ITEM){
return Object.assign({}, state, {
baskeitems: state.baskeitems.filter((item)=>{return(item!==action.payload)})
})
}
else{
return state;
}
}
}
export default Rootreducer;