Skip to content

Commit 74f2caf

Browse files
committed
React router scroll to top on route visit
1 parent 350e299 commit 74f2caf

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/App.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import About from './pages/AboutMe';
77
import Projects from './pages/Projects';
88
import Contact from './pages/Contact';
99
import ProjectSingle from './pages/ProjectSingle';
10+
import ScrollToTop from './components/ScrollToTop';
1011

1112
function App() {
1213
return (
1314
<div className=" bg-secondary-light dark:bg-primary-dark transition duration-300">
1415
<Router>
16+
<ScrollToTop />
1517
<AppHeader></AppHeader>
1618
<Routes>
1719
<Route path="/" element={<Home />} />

src/components/ScrollToTop.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
const ScrollToTop = () => {
5+
const { pathname } = useLocation();
6+
7+
useEffect(() => {
8+
window.scrollTo(0, 0);
9+
}, [pathname]);
10+
return null;
11+
};
12+
13+
export default ScrollToTop;

0 commit comments

Comments
 (0)