【发布时间】:2021-05-11 02:32:33
【问题描述】:
我正在if 条件下更新我的nudgeList 状态。如果条件是true,我将一些对象插入到我的空状态数组中
const [avgOrderCount, setAvgOrderCount] = useState('')
const [productCategory, setProductCategory] = useState('')
const [ecoPackage, setEcoPackage] = useState('')
const [domesticDelivery, setDomesticDelivery] = useState('')
const [foreignSalesTypeList, setforeignSalesTypeList] = useState([]
const [nudgeList, setNudgeList] = useState([])
if (Number(avgOrderCount) <= 9999) {
if ((productCategory === ProductCategoryType.BEAUTY || productCategory === ProductCategoryType.SOME_BEAUTY) && ecoPackage === ecoPackageType.YES && domesticDelivery === domesticDeliveryType.YES && !foreignSalesTypeList.includes('open_market') && !foreignSalesTypeList.includes('export') && !foreignSalesTypeList.includes('none') && foreignSalesTypeList.length >= 1) {
setNudgeList(list => [...list, eco, sample, orderSheet, wrongDelivery, oliveYoung, api, fifo, manager])
}
}
但是,在我测试我的代码后,我得到了错误
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
我怀疑问题在于我如何更新我的状态
setNudgeList(list => [...list, eco, sample, orderSheet, wrongDelivery, oliveYoung, api, fifo, manager])
但我很困惑为什么当我只有一个 if 条件时会触发无限循环。
如何解决此错误?
【问题讨论】:
标签: reactjs react-hooks