always scroll to top on route change, except on back nav
This commit is contained in:
@ -1,12 +1,16 @@
|
||||
import { useEffect } from "react";
|
||||
import { useLocation } from "react-router-dom";
|
||||
import { useLocation, useNavigationType } from "react-router-dom";
|
||||
|
||||
export default function ScrollToTop() {
|
||||
const { pathname } = useLocation();
|
||||
const navigationType = useNavigationType(); // This hook is available in React Router v6
|
||||
|
||||
useEffect(() => {
|
||||
window.scrollTo(0, 0);
|
||||
}, [pathname]);
|
||||
if (navigationType !== "POP") {
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
// Only scrolls to top on PUSH or REPLACE, not on POP
|
||||
}, [pathname, navigationType]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user