title: Is useRef Hook a must to set and clear intervals in React?
body: <p>I'm currently understanding the useRef hook and its usage. Accessing the DOM is a pretty straight forward use case which I understood. The second use case is that a ref behaves like an instance field in class components. And the <a href="https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables" rel="noreferrer">react docs</a> provide an example of setting and clearing a time interval from a click handler. I want to know, if cancelling the time interval from a click handler is not required, can we set and clear intervals with local variables declared within useEffect like below? Or is using a ref as mentioned in the docs always the approach to be taken?</p>
  
  <pre><code>useEffect(() =&gt; {
   const id = setInterval(() =&gt; {
    // ...
    });
    return () =&gt; {
      clearInterval(id);
    };
  })
  </code></pre>