Advertisement

Responsive Advertisement

Scrolling the window to the top of the page whenever the route changes.




Here's a basic example:


    import React, { useEffect } from "react";
    import { useLocation } from "react-router-dom";

    const ScrollToTopOnPageChange = () => {
      const { pathname } = useLocation();

      useEffect(() => {
        window.scrollTo(0, 0);
      }, [pathname]);

      return null;
    };

    export default ScrollToTopOnPageChange;

Post a Comment

0 Comments