title: useEffect exhaustive-deps warning: what possible issues is this trying to avoid?
body: <p>I realize there have been many questions about React's useEffect hook's dependency array, and the eslint warning that may arise from missing dependencies.  Some other good discussions about this:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/55840294/how-to-fix-missing-dependency-warning-when-using-useeffect-react-hook">How to fix missing dependency warning when using useEffect React Hook</a></li>
  <li><a href="https://stackoverflow.com/questions/65830990/react-useeffect-missing-dependency/65831064#65831064">React useEffect Missing Dependency</a></li>
  <li><a href="https://stackoverflow.com/questions/67233858/react-useeffect-missing-dependecy/67233950#67233950">React useEffect missing dependecy?</a></li>
  <li><a href="https://stackoverflow.com/questions/64148503/react-hook-useeffect-has-a-missing-dependency-with-useeffect">React Hook useEffect has a missing dependency with useEffect</a></li>
  </ul>
  <p>Although the causes &amp; solutions for these warnings are very clear, one thing I'm struggling to grasp is the <em>why</em>.  First, to quickly summarize, my understanding is that useEffect as three general purposes:</p>
  <ul>
  <li>If its dependency array is omitted, it acts like both componentDidMount and componentDidUpdate: it runs the first time the component is rendered, and all subsequent renders.</li>
  <li>If its dependency array is empty, it acts like componentDidMount: it can be used to run first-time initialization code (e.g. fetching initial data from the server, etc).</li>
  <li>Otherwise, you can include in its dependency array any variables that you want it to 'watch'; it will act like componentDidMount, and then like componentDidUpdate when those variables change.</li>
  <li>(For completeness: its return value can also be used to implement componentWillUnmount behavior, but that's beyond the scope of this issue).</li>
  </ul>
  <p>I understand that eslint will complain if useEffect references any functions (or other variables) which are declared outside of useEffect.  Per the above links, there are various ways to resolve these warnings: Move the function definition inside useEffect (if nothing outside useEffect needs to call it); add the function to the dependency array &amp; memoize it with useCallback; disable the warning with <code>// eslint-disable-next-line</code> or <code>// eslint-disable-line react-hooks/exhaustive-deps</code>; etc.  These all make sense. What I'm struggling to understand is WHY it even complains about this.</p>
  <p>So, my specific questions:</p>
  <ol>
  <li><p>Practically speaking, I can't understand why you would <em>ever</em> want a reference to a local function in the dependency array. For example, if you have <code>useEffect(() =&gt; { checkCurrentUser() }, [])</code>, and checkCurrentUser needs to be defined outside useEffect because it's called elsewhere, checkCurrentUser will be re-defined on every component render (which is why if we put it in the array, we should memoize it w/ useCallback).  But to me, it makes no logical sense why one would ever want this in the dependency array at all.  So why is this even a warning?  What is this warning actually helping to prevent? Throughout all the times I've seen the warning, in every case, the application has behaved exactly as I expected/intended.</p>
  </li>
  <li><p>If one of useEffect's primary purposes is to 'run once' like componentDidMount, and the way to achieve that behavior is to explicitly put an empty array, why does it hassle you if you specify an empty array? I can understand why it might make sense to warn if you have a dependency array that contains some references but not others - as in 'oops, you forgot some' - but the empty array is a specific, well-defined usage.  To me, it doesn't seem to make sense that it would tell you 'an empty array is an issue,' as that's the only way to achieve componentDidMount-type initialization.  It feels like the same thing as just warning you &quot;don't use componentDidMount().&quot;  Why does it complain about an empty array, if that's supposedly one of the 3 main uses of useEffect?</p>
  </li>
  </ol>
  <p>Again, I do understand the circumstances in which the warning appears, and the ways to resolve it.  I just struggle to understand the benefit of even having it, (1) in the case of functions (which we definitively know will change on every single render), and (2) in the case of an empty array (which are a specific usage).</p>