1 回答

TA贡献1830条经验 获得超3个赞
我在您的 useEffect 中发现的问题。
使用isMount变量来跟踪元素的挂载状态,并能够避免在它已经卸载时更新反应状态。这应该删除警告。
最终代码:
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]);
添加回答
举报