【发布时间】:2019-04-24 20:42:01
【问题描述】:
最近,我将购物车 Appt 更新为 CRA 3,其中包括 eslint-plugin-react-hooks useEffect 。适当地,它强制依赖于 useEffect 中的第二个参数数组。
我的意图是只在挂载时运行,所以我之前使用过 [] 并且它按预期工作,但现在它添加了这些依赖项并且不会在加载时运行。我知道我可以全局或单独关闭此 eslint 规则,但我宁愿知道在反应中完成此操作的适当方法。
const CartItem = ({ inventoryItems, dispatch, item }) => {
const invItem = inventoryItems.find(invItem => invItem.id === item.id);
useEffect(() => {
const setWarnings = (item) => {
let warnings = [];
if (item.quantity > invItem.quantity) {
warnings.push(`Note quantity of requested ${
item.name
}s was reduced by ${item.quantity -
invItem.quantity} due to sold inventory since it was placed in cart.`);
item.quantity = invItem.quantity;
}
if (item.price !== invItem.price) {
warnings.push(`Note price for ${
item.name
} has changed since it was placed in cart (${
invItem.price > item.price ? "+ " : ""
} ${formatPrice(invItem.price - item.price)}).`);
item.price = invItem.price;
}
}
setWarnings(item)
},[invItem.price, invItem.quantity, item])
return (/* here I will display my item and below it map warnings array */)
}
【问题讨论】:
标签: reactjs eslint create-react-app