title: Next.js render video only on desktop: useEffect can't perform a React state update on an unmounted component
body: <p>I know there are multiple similar questions here already, but I can't see which asynchronous fix I need to make in order to make this work.</p>
  <p>I have a Next.js website where I want to render a video but only when we're on desktop. I don't want the video at all to be there on mobile to save the user from downloading it in the first place, and in order to do that I wrote my own React hook to keep track of the window size.</p>
  <p>I can use the <code>isDesktop</code> in my code and it all works, and since I am still in development phase I added the window resize listener so I can easily test things by just resizing my window rather than reloading the page each time.</p>
  <p>Everything works as expected, except for this dreaded message:</p>
  <pre><code>Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
  </code></pre>
  <p>which is driving me nuts. I have this code:</p>
  <pre><code>import { useState, useEffect } from 'react';
  
  export function useWindowSize() {
      const isBrowser = typeof window !== 'undefined';
      const [isDesktop, setIsDesktop] = useState&lt;boolean&gt;(false);
  
      function update() {
          setIsDesktop(window.innerWidth &gt;= 768);
      }
  
      useEffect(() =&gt; {
          if (isBrowser) {
              update();
              window.addEventListener('resize', update, false);
              return () =&gt; {
                  window.removeEventListener('resize', update, false);
              };
          }
      }, [isBrowser]);
  
      return { isDesktop };
  }
  </code></pre>
  <p>and as soon as I comment out the <code>update</code> call in the <code>useEffect</code>, the message disappears. But then I no longer get the initial state (meaning, it will not render the video on my desktop page until i start resizing the window, and that's not what should happen).</p>