I am using React at the moment, when I click on a button that takes me to another page, by default it goes to the bottom of my page. Tried using window.scrollTo(0, 0); but it doesn't seem to work.
const handleRedirection = () => {
history.push(/home);
window.scrollTo(0, 0);
};
The page I would like to go to currently has css set as
height: calc(100vh - 50px);
overflow: scroll;
Was wondering is there a solution to fix this issue. Thanks! ๐
you can scroll to top based on route change
try this:
import { useLocation } from "react-router-dom";
//your codes code
const { pathname } = useLocation();
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
//your codes
also, you can use it as a top-level component to apply this to all your pages