title: React scroll to top on page refresh - don't restore position
body: <p>I'm currently trying to scroll to top on page refresh. So far I've been able to implement scroll to top on route change, but not <strong>on page refresh - somehow react always restores its previous position</strong>. How can I prevent that from happening? </p>
  
  <p>That's how I implemented scroll to top (thought <code>window.scrollTo(0, 0)</code> inside of <code>componenDidMount()</code> would help, but it didn't):</p>
  
  <pre><code>class ScrollToTop extends Component {
  
      componentDidMount() {
          window.scrollTo(0, 0);
      }
  
      componentDidUpdate(prevProps, prevState, snapshot) {
          if (this.props.location.pathname !== prevProps.location.pathname) {
              window.scrollTo(0, 0);
          }
      }
  
      render() {
          return null;
      }
  }
  
  export default withRouter(ScrollToTop);
  </code></pre>
  
  <pre><code>class App extends Component {
  
      render() {
          return (
              &lt;BrowserRouter&gt;
                  &lt;ScrollToTop/&gt;
                  &lt;Header/&gt;
                  &lt;Home/&gt;
                  &lt;Projects/&gt;
  
                  &lt;div className="follow-cursor"/&gt;
              &lt;/BrowserRouter&gt;
          );
      }
  }
  
  export default App;
  </code></pre>