{"id":387,"hash":"f5796a70ec2a0e15dbdfc4569f2b4612122ce428a2aca8da7c76c4b409e60545","pattern":"How to fix missing dependency warning when using useEffect React Hook","full_message":"With React 16.8.6 (it was good on previous version 16.8.3), I get this error when I attempt to prevent an infinite loop on a fetch request:\n\n./src/components/BusinessesList.js\nLine 51:  React Hook useEffect has a missing dependency: 'fetchBusinesses'.\nEither include it or remove the dependency array  react-hooks/exhaustive-deps\n\nI've been unable to find a solution that stops the infinite loop. I want to stay away from using useReducer().  I did find this discussion [ESLint] Feedback for 'exhaustive-deps' lint rule #14920 where a possible solution is You can always // eslint-disable-next-line react-hooks/exhaustive-deps if you think you know what you're doing. I'm not confident in what I'm doing, so I haven't tried implementing it just yet.\n\nI have this current setup, React hook useEffect runs continuously forever/infinite loop and the only comment is about useCallback() which I'm not familiar with.\n\nHow I'm currently using useEffect() (which I only want to run once in the beginning similar to componentDidMount()):\n\nuseEffect(() => {\n    fetchBusinesses();\n  }, []);\n\nconst fetchBusinesses = () => {\n    return fetch(\"theURL\", {method: \"GET\"}\n    )\n      .then(res => normalizeResponseErrors(res))\n      .then(res => {\n        return res.json();\n      })\n      .then(rcvdBusinesses => {\n        // some stuff\n      })\n      .catch(err => {\n        // some error handling\n      });\n  };","ecosystem":"npm","package_name":"reactjs","package_version":null,"solution":"If you aren't using fetchBusinesses method anywhere apart from the effect, you could simply move it into the effect and avoid the warning\n\nuseEffect(() => {\n    const fetchBusinesses = () => {\n       return fetch(\"theURL\", {method: \"GET\"}\n    )\n      .then(res => normalizeResponseErrors(res))\n      .then(res => {\n        return res.json();\n      })\n      .then(rcvdBusinesses => {\n        // some stuff\n      })\n      .catch(err => {\n        // some error handling\n      });\n  };\n  fetchBusinesses();\n}, []);\n\nIf however you are using fetchBusinesses outside of the effect, you must note two things\n\nIs there any issue with you not passing fetchBusinesses as a method when it's used during mount with its enclosing closure?\nDoes your method depend on some variables which it receives from its enclosing closure? This is not the case for you.\nOn every render, fetchBusinesses will be re-created and hence passing it to useEffect will cause issues. So first you must memoize fetchBusinesses if you were to pass it to the dependency array.\n\nTo sum it up I would say that if you are using fetchBusinesses outside of useEffect you can disable the rule using // eslint-disable-next-line react-hooks/exhaustive-deps otherwise you can move the method inside of useEffect\n\nTo disable the rule you would write it like\n\nuseEffect(() => {\n   // other code\n   ...\n \n   // eslint-disable-next-line react-hooks/exhaustive-deps\n}, [])","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/55840294/how-to-fix-missing-dependency-warning-when-using-useeffect-react-hook","votes":891,"created_at":"2026-04-19T04:51:05.196036+00:00","updated_at":"2026-04-19T04:51:24.108434+00:00"}