title: Detecting scrolling direction on the page - updating prev value
body: <p>I'm using React and I need to get a scroll direction in order to do some stuff. I have a working code but I'm clueless on how to store and <strong>update</strong> previous scroll position. </p>
  
  <p>Here's my code: </p>
  
  <pre><code>componentDidMount(){
      const prev = window.scrollY;
      window.addEventListener('scroll', e =&gt; this.handleNavigation(e, prev);
  }
  </code></pre>
  
  <p>Visible problem here is that componentDidMount <strong>fires only once</strong> so when I try to do the following: </p>
  
  <pre><code>handleNavigation = (e, prev) =&gt;{
      const window = e.currentTarget;
  
      if(prev &gt; window.scrollY){
          console.log("scrolling up");
      }
      else if(prev &lt; window.scrollY){
          console.log("scrolling down");
      }
  };
  </code></pre>
  
  <p>It compares values properly but <strong>prev never changes</strong>, so it doesn't work as intended. How do I make it work? </p>
  
  <p>Do I put prev inside the interval to update the value or something crazy like that? </p>