'How to remove an item from Local Storage when clicking on it? [duplicate]
I want to delete an item stored in Local Storage when it is clicked. But I don't know how to do that. Please help me to solve this problem. Thanks a lot!!
the code right here:
const cartSlice = createSlice({
name: 'cart',
initialState: JSON.parse(localStorage.getItem('item')) || {
products: [],
quantity: 0,
total: 0,
},
reducers: {
addProduct: (state, action) => {
state.products.push(action.payload);
state.quantity += 1;
state.total += action.payload.price * action.payload.quantity;
localStorage.setItem('item', JSON.stringify(state));
},
deleteItem: (state, action) => {
state.products = state.products.filter((item) => {
//
});
state.quantity -= 1;
state.total -= action.payload.price * action.payload.quantity;
},
reset: (state) => {
return state;
},
},
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|