使用React 16.8.6(之前的版本16.8.3没有问题),在我试图防止一个fetch请求进入无限循环时,我会遇到这个错误:
./src/components/BusinessesList.js
Line 51: React Hook useEffect has a missing dependency: 'fetchBusinesses'.
Either include it or remove the dependency array react-hooks/exhaustive-deps
我一直找不到停止无限循环的解决方案。我希望避免使用useReducer()
。我找到了这个讨论[ESLint] Feedback for 'exhaustive-deps' lint rule #14920,其中一个可能的解决方案是如果你认为自己知道在做什么,你可以总是 // eslint-disable-next-line react-hooks/exhaustive-deps。
我对自己正在做的事情没有信心,所以我还没有尝试实现它。
我目前的设置是这样的:React hook useEffect runs continuously forever/infinite loop,唯一的评论是关于useCallback()
,我不熟悉它。
我目前如何使用useEffect()
(我只想在开始时运行一次,类似于componentDidMount()
):
useEffect(() => {
fetchBusinesses();
}, []);
const fetchBusinesses = () => {
return fetch("theURL", {method: "GET"}
)
.then(res => normalizeResponseErrors(res))
.then(res => {
return res.json();
})
.then(rcvdBusinesses => {
// some stuff
})
.catch(err => {
// some error handling
});
};