【发布时间】:2021-10-18 13:34:50
【问题描述】:
为什么 eslint 会抛出错误( no-unused-expression )。我只是想检查数组是否为空,然后执行调度。
useEffect(() => {
let totalPrice = 0;
// eslint-disable-next-line no-unused-expressions
cart.length
? cart.forEach((item: AsideItemInterface) => {
totalPrice += item.quantity * item.price;
dispatch(setTotal(totalPrice));
})
: dispatch(setTotal(0));
}, [cart]);
如果我删除 eslint-disable-next-line,则会出错。
【问题讨论】:
-
购物车的值是异步的吗?试试
cart?.length -
错误是什么?还有购物车在哪里定义?什么是购物车初始值?
标签: eslint