title: What's the difference between these solutions when dealing with cleaning up of unmounted components? useRef vs boolean variable
body: <p>I've learned that to prevent memory leaks, I'm supposed to clean up after useEffect hooks which call async functions to update state.</p>
  <p>I have found a couple of solutions for this;</p>
  <p>First one, to use a simple, boolean variable:</p>
  <pre><code>useEffect(() =&gt; {
  let isSubscribed = true;
  
  const runEffect = async () =&gt; {
    const data = await getSomething();
    isSubscribed &amp;&amp; setState(data)
  };
  
  runEffect();
  
  return () =&gt; {
        isSubscribed = false;
      };
    }, []);
  </code></pre>
  <p>And this method using the useRef hook:</p>
  <pre><code>const _isMounted = useRef(true); 
  
    useEffect(() =&gt; {
  const runEffect = async () =&gt; {
        const data = await getSomething();
        _isMounted.current &amp;&amp; setState(data)
      };
  
      runEffect();
  
      return () =&gt; { 
          _isMounted.current = false;
      }
    }, []);
  </code></pre>
  <p>Is there a big enough difference between the two methods? If so, which is better?</p>