Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

handling scrolling on react router transitions

import { useEffect } from 'react';
import { withRouter } from 'react-router-dom';

function ScrollToTop({ history }) {
  useEffect(() => {
    const unlisten = history.listen(() => {
      window.scrollTo(0, 0);
    });
    return () => {
      unlisten();
    }
  }, []);

  return (null);
}

export default withRouter(ScrollToTop);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #handling #scrolling #react #router #transitions
ADD COMMENT
Topic
Name
4+8 =