1 回答

TA貢獻1830條經(jīng)驗 獲得超3個贊
我在您的 useEffect 中發(fā)現(xiàn)的問題。
使用isMount變量來跟蹤元素的掛載狀態(tài),并能夠避免在它已經(jīng)卸載時更新反應狀態(tài)。這應該刪除警告。
最終代碼:
useEffect(() => {
let isMount = true
axios.get("http://localhost:4000/products/")
.then(res => {
if (!isMount) return // If element has unmount, dont update the state
setProducts(res.data);
setCurrentProducts(products.slice(offset, offset + pageLimit));
}).catch(function(err) {
if (!isMount) return // If element has unmount, dont update the state
setIsError(true);
})
return () => {
isMount = false
}
}, [offset, products]);
添加回答
舉報