I need to refresh my cart page everytime, when products.length is 0 but I can't figure out how to write that if condition, now it writes unreachable code. I think it is something simple, so I am here to ask.
switch (action.type){
case "REMOVE_PRODUCT":
return products.filter((p) => p.id !== action.productId);
if (products.length === 0) {
window.location.reload();
}
}
Well, you code is unreachable since it is after a return statement, so change it to something like this:
switch (action.type) {
case "REMOVE_PRODUCT":
const data = products.filter((p) => p.id !== action.productId);
if (data.length === 0) {
window.location.reload();
}
return data;
}
Note: If you are using redux, as it seems, maybe is better to handle this logic in the component that uses the data instead of here.
You are receiving code unreachable as you are returning before checking into the IF block.