I am updating my nudgeList state within an if condition. If the condition is true, I insert a number of objects into my empty state array
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])
}
}
However, after I test my code I get the error
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
I suspect the problem lies with how I am updating my state
setNudgeList(list => [...list, eco, sample, orderSheet, wrongDelivery, oliveYoung, api, fifo, manager])
But I am confused why this is triggering an infinite loop when I just have one if condition.
How do I resolve this error?