当products.length为0时,我每次都需要刷新我的购物车页面,但我不知道如何编写if条件,现在它编写无法访问的代码。我觉得这很简单,所以我在这里问。
switch (action.type){
case "REMOVE_PRODUCT":
return products.filter((p) => p.id !== action.productId);
if (products.length === 0) {
window.location.reload();
}
}
好吧,你的代码是不可访问的,因为它在return
语句之后,所以把它改成这样:
switch (action.type) {
case "REMOVE_PRODUCT":
const data = products.filter((p) => p.id !== action.productId);
if (data.length === 0) {
window.location.reload();
}
return data;
}
注意:如果您使用的是redux
,那么最好在使用数据的组件中处理此逻辑,而不是在这里。
在签入IF块之前返回时,您正在接收code unreachable
。