admin管理员组文章数量:1122832
I’m working on a React app using react-router-dom. I want the page to scroll to the top every time the route changes. Here’s what I’ve tried:
Used a custom hook with useEffect:
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
const useScrollToTop = () => {
const { pathname } = useLocation();
useEffect(() => {
console.log("Attempting to scroll to top"); // Logs correctly
window.scrollTo({
top: 0,
behavior: "auto",
});
console.log("Scroll executed"); // Also logs correctly
}, [pathname]);
};
export default useScrollToTop;
Added the hook in my App.tsx:
import useScrollToTop from './hooks/useScrollToTop';
const App = () => {
useScrollToTop();
return (
<BrowserRouter>
<Routes>
{/* My routes */}
</Routes>
</BrowserRouter>
);
};
Verified that window.scrollTo executes (logs appear), but the page doesn’t scroll to the top.
I also tested with:
a. useLayoutEffect instead of useEffect.
b. Forcing a delay using setTimeout.
c. Checking for scrollable elements (html, body, or a custom container).
d. CSS fixes like ensuring html, body { height: auto; overflow: visible; }.
e. Despite all this, the page still doesn’t scroll to the top. Is there anything I’m missing or any interference I should check for?
I’m working on a React app using react-router-dom. I want the page to scroll to the top every time the route changes. Here’s what I’ve tried:
Used a custom hook with useEffect:
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
const useScrollToTop = () => {
const { pathname } = useLocation();
useEffect(() => {
console.log("Attempting to scroll to top"); // Logs correctly
window.scrollTo({
top: 0,
behavior: "auto",
});
console.log("Scroll executed"); // Also logs correctly
}, [pathname]);
};
export default useScrollToTop;
Added the hook in my App.tsx:
import useScrollToTop from './hooks/useScrollToTop';
const App = () => {
useScrollToTop();
return (
<BrowserRouter>
<Routes>
{/* My routes */}
</Routes>
</BrowserRouter>
);
};
Verified that window.scrollTo executes (logs appear), but the page doesn’t scroll to the top.
I also tested with:
a. useLayoutEffect instead of useEffect.
b. Forcing a delay using setTimeout.
c. Checking for scrollable elements (html, body, or a custom container).
d. CSS fixes like ensuring html, body { height: auto; overflow: visible; }.
e. Despite all this, the page still doesn’t scroll to the top. Is there anything I’m missing or any interference I should check for?
1 Answer
Reset to default 0Issue
Your custom hook is clearly being called outside the router:
const App = () => {
useScrollToTop(); // <-- needs routing context
return (
<BrowserRouter> // <-- provides routing context
<Routes>
{/* My routes */}
</Routes>
</BrowserRouter>
);
};
So it can't possibly have access to the routing context that BrowserRouter
provides to the useLocation
hook and all other RRD hooks and components called/rendered within its sub-ReactTree, e.g. its children.
In fact, there should be a console error about that useLocation
hook call:
Solution Suggestion
Move the useScrollToTop
hook to a component rendered within the router so that it can properly access the routing context.
Below I have just converted the hook into a React component so that it is renderable as JSX within the router and able to call side-effects.
Example:
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
const ScrollToTop = () => {
const { pathname } = useLocation();
useEffect(() => {
window.scrollTo({
top: 0,
behavior: "auto",
});
}, [pathname]);
return null;
};
export default ScrollToTop;
const App = () => {
return (
<BrowserRouter> // <-- provides routing context
<ScrollToTop /> // <-- can access routing context
<Routes>
{/* My routes */}
</Routes>
</BrowserRouter>
);
};
本文标签: reactjsReact windowscrollTo not working on route changeStack Overflow
版权声明:本文标题:reactjs - React window.scrollTo not working on route change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302461a1931540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论